poj 2229 Sumsets
来源:互联网 发布:淘宝美工需要什么资质 编辑:程序博客网 时间:2024/05/20 05:03
Sumsets
Time Limit: 2000MS Memory Limit: 200000KTotal Submissions: 18601 Accepted: 7277
Description
Farmer John commanded his cows to search for different sets of numbers that sum to a given number. The cows use only numbers that are an integer power of 2. Here are the possible sets of numbers that sum to 7:
1) 1+1+1+1+1+1+1
2) 1+1+1+1+1+2
3) 1+1+1+2+2
4) 1+1+1+4
5) 1+2+2+2
6) 1+2+4
Help FJ count all possible representations for a given integer N (1 <= N <= 1,000,000).
1) 1+1+1+1+1+1+1
2) 1+1+1+1+1+2
3) 1+1+1+2+2
4) 1+1+1+4
5) 1+2+2+2
6) 1+2+4
Help FJ count all possible representations for a given integer N (1 <= N <= 1,000,000).
Input
A single line with a single integer, N.
Output
The number of ways to represent N as the indicated sum. Due to the potential huge size of this number, print only last 9 digits (in base 10 representation).
Sample Input
7
Sample Output
6
Source
数学题,找规律。
首先,当i为奇数的时候,不难发现,最后的总数总是与前一个偶数的结果相同,是因为,二的幂都是偶数,要想凑成奇数的话,必须使用1,所以,最后的结果总是等于前面的偶数的结果在每一行的首位添加一个一。a[i]=a[i-1];
当i为偶数的时候,仔细观察所有的式子不难发现,其中以一开头的行数等于a[i-1],剩下的以2,4,8,16......开头的行数将其全部除以2后发现,等于a[i/2]的行数,然后将其相加即可得到最后结果,即a[i-1]+a[i/2];
最后不要忘记输出最后九位,%mod;
当然,使用dp方法也可以得出答案
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>using namespace std;const int mod=1000000000;int a[1000010];int main(){ a[1]=1; a[0]=1; for(int i=2;i<=1000000;i++){ if(i&1){ a[i]=a[i-1]%mod; } else{ a[i]=(a[i-1]%mod+a[i/2]%mod)%mod; } } int n; while(cin>>n){ cout << a[n] <<endl; } return 0;}
0 0
- POJ 2229 Sumsets
- poj 2229 Sumsets
- [POJ] 2229 Sumsets
- sumsets poj 2229
- POJ-2229-Sumsets
- poj 2229 Sumsets
- poj 2229 Sumsets DP
- POJ 2229 Sumsets DP
- poj 2229 Sumsets
- poj-2229-Sumsets
- Sumsets poj 2229 c++
- POJ-2229 Sumsets DP
- poj 2229 Sumsets
- poj 2229 Sumsets【DP】
- POJ 2229 Sumsets
- poj 2229 Sumsets
- POJ 2229 Sumsets
- POJ 2229:Sumsets
- python从入门到..........
- android源码内置语音信箱*86
- Node.js 使用 npm 有问题,转cnpm镜像
- python学习2——if语句、elif、if嵌套、while循环、while嵌套
- 【类反射】类反射的一个引例(USB)
- poj 2229 Sumsets
- Robot Framework自动化测试-元素定位之id、name
- 图片的每点击一次旋转90度, filter和css3属性
- SICP 1.11
- POJ3666 Making the Grade(DP,离散化 待整理)
- [CoffeeBot] 配置CoffeeBot的网页服务器
- 小技巧,小知识
- SQL事物与事物隔离级别
- Mac系统终端命令行不执行命令 总出现command not found解决方法