Sumsets
来源:互联网 发布:网络安全技术的建议 编辑:程序博客网 时间:2024/05/16 12:37
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).
A single line with a single integer, N.
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).
7
6
思路:通过从1开始分析可得:
N=1:1--1种方法
N=2:1+1
2--2种方法
N=3:1+1+1
1+2--2种方法
N=4:1+1+1+1
1+1+2
2+2一下为a[4/2]项中的每项乘以2
4--4种方法
N=5:1+1+1+1+1
1+1+1+2
1+2+2
1+4--4种方法
N=6:1+1+1+1+1+1
1+1+1+1+2
1+1+2+2
1+1+4
2+2+2一下为a[6/2]项中的每项乘以2
2+4--6种方法
N=7: 1+1+1+1+1+1+1 1+1+1+1+1+2 1+1+1+2+2 1+1+1+4 1+2+2+2 1+2+4 --6种方法
N=8: 1+1+1+1+1+1+1 +11+1+1+1+1+1+2 1+1+1+1+2+2 1+1+1+1+4 1+1+2+2+2 1+1+2+4
2+2+2+2一下为a[8/2]项中的每项乘以2
2+2+4
4+4
8--10种方法
N=9: 1+1+1+1+1+1+1+1 +11+1+1+1+1+1+1+2 1+1+1+1+1+2+2 1+1+1+1+1+4 1+1+1+2+2+2 1+1+1+2+4
1+2+2+2+2
1+2+2+4
1+4+4
1+8--10种方法
有分析可得:状态方程a[n]=a[n-2]+a[n/2];
代码实现:
#include<cstdio>#include<cstring>using namespace std;int main(){ int n; scanf("%d",&n); int num[1000001]; num[0]=num[1]=1; for(int i=2;i<=n;i++) { num[i]=num[i-2]+num[i/2]; num[i]=num[i]%1000000000; } printf("%d\n",num[n]); return 0;}
阅读全文
0 0
- Sumsets
- Sumsets
- Sumsets
- Sumsets
- Sumsets
- Sumsets
- 10125 - Sumsets(*****)
- 10125 - Sumsets
- uva10125 - Sumsets
- uva10125 Sumsets
- HDU2709 Sumsets
- 10125 - Sumsets
- POJ_2229 Sumsets
- poj2229 Sumsets
- poj2229 Sumsets
- POJ2229 Sumsets
- POJ2229 Sumsets
- POJ2229 Sumsets
- Largest Rectangle in Histogram
- centos开机启动自定义脚本
- 层次聚类算法的原理及实现Hierarchical Clustering
- 遗传算法计算一元函数最大值,工具:matlab
- xshell 隧道 连接mysql
- Sumsets
- MySQL5.7免安装配置
- JS中的prototype的作用
- 关于set/multiset和map/multimap总结
- hdu-2700-Parity
- Python语言实现哈夫曼编码
- redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refu
- CSS学习之旅
- python发送邮件