2013暑假集训B组训练赛第二场

B - Funky Numbers
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

As you very well know, this year's funkiest numbers are so called triangular numbers (that is, integers that are representable as , where k is some positive integer), and the coolest numbers are those that are representable as a sum of two triangular numbers.

A well-known hipster Andrew adores everything funky and cool but unfortunately, he isn't good at maths. Given number n, help him define whether this number can be represented by a sum of two triangular numbers (not necessarily different)!

Input

The first input line contains an integer n (1 ≤ n ≤ 109).

Output

Print "YES" (without the quotes), if n can be represented as a sum of two triangular numbers, otherwise print "NO" (without the quotes).

Sample Input

Input
256
Output
YES
Input
512
Output
NO


一开始开了个10^9的数组,结果mle
后来改了还是tle
然后继续怎么都过不了
然后一把乱改,结果改好了。。。= =
心酸;
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

const long long MAXN = 1000000005;

int num;
int f[50000];
int n;

void build()
{
    n = 0;
    int k;
    for(int i = 1; k < MAXN; i++)
    {
        k = i*(i+1)/2;
        f[n++]= k;
    }
}


int main()
{
    build();
    n--;
    while(~scanf("%d",&num))
    {
        bool is = 0;
        for(int i = 0; f[i] < num; i++)
        {
            int id = lower_bound(f, f + n,num - f[i]) - f;

            if(f[id] == num - f[i])
            {
                is = true;
                break;
            }
        }
        printf("%s
", is ? "YES" : "NO");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/wejex/p/3219485.html