Funky Numbers CodeForces - 192A

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).

Example

Input

256

Output

YES

Input

512

Output

NO

Note

In the first sample number .

In the second sample number 512 can not be represented as a sum of two triangular numbers.

写得有点丑~~~

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<math.h>
 5 #include<string>
 6 using namespace std;
 7 typedef long long ll;
 8
 9 int n;
10 ll cal(ll ans){
11    return ans*(ans+1)/2;
12 }
13
14 int main()
15 {  while(~scanf("%d",&n)){
16        int flag=0;
17        for(int i=1;i<=sqrt(2*n)+1;i++){
18               ll sum=n-cal(i);
19               ll l=i,r=sqrt(2*n)+1;
20               ll mid;
21               while(l<r){
22                     mid=(l+r)/2;
23                     if(cal(mid)==sum){
24                          flag=1;
25                          break;
26                     }
27                     if(cal(mid)<sum) l=mid;
28                     else r=mid;
29                     if(r-l==1){
30                          if(cal(r)==sum||cal(l)==sum){
31                              flag=1;
32                              break;
33                          }
34                          else break;
35                     }
36               }
37               if(flag) break;
38        }
39        if(flag) printf("YES\n");
40        else printf("NO");
41    }
42 }
时间: 2024-10-05 19:20:49

Funky Numbers CodeForces - 192A的相关文章

D. Arpa and a list of numbers Codeforces Round #432 (Div. 2, based on IndiaHacks Final Round 2017)

http://codeforces.com/contest/851/problem/D 分区间操作 1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstring> 5 #include <time.h> 6 #include <string> 7 #include <set> 8 #include <map> 9

Really Big Numbers CodeForces - 817C (数学规律+二分)

C. Really Big Numbers time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Ivan likes to learn different things about numbers, but he is especially interested in really big numbers. Ivan thinks

Jzzhu and Numbers CodeForces - 449D (高维前缀和,容斥)

大意: 给定集合a, 求a的按位与和等于0的非空子集数. 首先由容斥可以得到 $ans = \sum \limits_{0\le x <2^{20}} (-1)^{\alpha} f_x$, 其中$\alpha$为$x$二进制中$1$的个数, $f_x$表示与和等于$x$的非空子集数. $f_x$是一个$20$维前缀和, 按传统容斥做法的话显然要超时, 可以每次求一维的和, 再累加 比方说对于2维前缀和, 用容斥的求法是这样 for (int i=1; i<=n; ++i) { for (in

Jzzhu and Numbers CodeForces - 449D (容斥,dp)

大意: 给定集合a, 求a的按位与和等于0的非空子集数. 为了方便表述, 把每个数看成一个二进制位表示的集合, 例如十进制的$10$就看做集合$\{1,3\}$. 假设给定数的范围在$[0,2^{m})$内, 记$U=\{0,1,2,\cdots,m-1\}$. 首先根据容斥可以得到 $$ans=\sum\limits_{S\subseteq 2^{U}}(-1)^{|S|}(2^{f_S}-1) \tag{1}$$ 其中$f_S=\sum\limits_{T\in a}[T\supseteq

2015 百度之星初赛 1 2 2015ACM/ICPC亚洲区上海站 codeforces 851

A - 超级赛亚ACMer HDU - 5246 n m k m >=max( a[i] ); m < min(a[i]); 先判断掉 然后就可以找到最接近的 比m小的一个数 往上涨 看是否能行 O(n) #include<stdio.h> #include<string.h> #include<algorithm> #include<string> #include<iostream> #include<math.h>

codeforces630C

Lucky Numbers CodeForces - 630C 小希称只含7和8的数是幸运数,那么不超过n位的幸运数有多少个? Input 一个整数 n (1 ≤ n ≤ 55) Output 输出幸运数的数量 Example Input 2 Output 6 sol:这和0,1有个屁区别 ----摘自yj大佬原话 结论非常显然小学生都知道 2n+1-2 #include <bits/stdc++.h> using namespace std; typedef long long ll; in

Codeforces 9C Hexadecimal&#39;s Numbers - 有技巧的枚举

2017-08-01 21:35:53 writer:pprp 集训第一天:作为第一道题来讲,说了两种算法, 第一种是跟二进制数联系起来进行分析: 第二种是用深度搜索来做,虽然接触过深度搜索但是这种题型还是我第一次见: 题目: 统计1~n之间有多少数字只由0,1构成 1 ≤ n ≤ 1e9 用深度搜索解决这种问题: 代码如下: #include <iostream> #include <map> using namespace std; map<int,int>vis;

[2016-04-14][codeforces][630][C][ Lucky Numbers]

时间:2016-04-14 23:12:27 星期四 题目编号:[2016-04-14][codeforces][630][C][ Lucky Numbers] 题目大意: 问n位数字以内的幸运数字有多少个 幸运数字:只含有7,8的数字 分析: 长度为i 的幸运数字,每一位有两种可能,7 , 8,那么长度为i的幸运数字总共有 $2^i$中可能 那么长度为n 以内的所有幸运数字 就是 $2^1 + 2^2 + - + 2^n$, #include<cstdio> using namespace

codeforces 446C DZY Loves Fibonacci Numbers(数学 or 数论+线段树)

In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation F1 = 1; F2 = 1; Fn = Fn - 1 + Fn - 2 (n > 2). DZY loves Fibonacci numbers very much. Today DZY gives you an array consisting of n integers: a1, a2, ...,