topcoder SRM 625 DIV2 A

题目

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 elements

  • ( x[0] * x[1] ) + x[2] = y

  • Each x[i] must be between -1000 and 1000, inclusive.

  • No x[i] can be equal to 0 or 1.

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)
6
                   
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)
11
                   
Returns: {2, 3, 5 }
             
2)
0
                   
Returns: {7, 10, -70 }
             
Note that 0 and 1 are not allowed, thus a result like 0                      * 0 + 0 would be incorrect.
3)
500
                   
Returns: {-400, -3, -700 }
             
Some or all of the returned numbers may be negative.
4)
2
                   
Returns: {2, 2, -2 }
             
5)
5
                   
Returns: {5, 2, -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.

分析

从反面,即什么时候不成立来思考

代码

#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];
    }
}



转载于:https://my.oschina.net/u/572632/blog/282575

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值