題目
Problem Statement |
|
You are given an int y. We are looking for any vector <int> x that satisfies the following constraints:
x has exactly three elementsios
( x[0] * x[1] ) + x[2] = ythis
Each x[i] must be between -1000 and 1000, inclusive.spa
No x[i] can be equal to 0 or 1.code
Find and return one such x.
If there are multiple valid solutions, you may return any of them. You may assume that for our constraints on y (specified below) at least one valid x always exists. |
Definition |
|
Class: |
AddMultiply |
Method: |
makeExpression |
Parameters: |
int |
Returns: |
vector <int> |
Method signature: |
vector <int> makeExpression(int y) |
(be sure your method is public) |
|
Limits |
|
Time limit (s): |
2.000 |
Memory limit (MB): |
256 |
|
Constraints |
- |
y will be between 0 and 500, inclusive. |
Examples |
0) |
|
|
|
Returns: {2, 2, 2 } |
2*2 + 2 = 6
Note that this is one of many possible solutions. Another solution is:
3*3 + (-3) = 6 |
|
|
1) |
|
|
|
2) |
|
|
|
Returns: {7, 10, -70 } |
Note that 0 and 1 are not allowed, thus a result like 0 * 0 + 0 would be incorrect. |
|
|
3) |
|
|
|
Returns: {-400, -3, -700 } |
Some or all of the returned numbers may be negative. |
|
|
4) |
|
|
|
5) |
|
|
|
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.orm
分析
從反面,即何時不成立來思考three
代碼
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
int aPA[3] = { 2, 2, 0};
int aPB[3] = { 3, 3, 0};
vector<int> PA(aPA, aPA + 3);
vector<int> PB(aPB, aPB + 3);
#define PAPOS 0
#define NOPA1 4
#define NOPA2 5
#define PBPOS 1
#define NOPB1 9
#define NOPB2 10
#define POSMAX 2
#define INIT_ANS() do{ ansTable[PAPOS] = PA; ansTable[PBPOS] = PB; }while(0)
#define MAXN 1000
#define MINN -1000
vector<int> ansTable[POSMAX];
class AddMultiply {
public:
vector <int> makeExpression(int);
};
vector <int> AddMultiply::makeExpression(int y) {
INIT_ANS();
if(y != NOPA1 && y != NOPA2){
ansTable[PAPOS][2] = y - 4;
return ansTable[PAPOS];
}else{
ansTable[PBPOS][2] = y - 9;
return ansTable[PBPOS];
}
}