Sumsets
2016-07-23 12:21
204 查看
HDU 2709Problem DescriptionFarmer 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+12) 1+1+1+1+1+23) 1+1+1+2+24) 1+1+1+45) 1+2+2+26) 1+2+4Help FJ count all possible representations for a given integer N (1 <= N <= 1,000,000). InputA single line with a single integer, N. OutputThe 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 Input7 Sample Output6 SourceUSACO 2005 January Silver思路:这题没什么思路,加上数学又学的不好.遂先打表找规律.(1-16)code:#include <iostream>#include<cstdio>#include<cmath>#include<cstring>#define M 100using namespace std;int ans[M];int ant;bool vis[10][10][10][10]; // 1 2 4 8void dfs(int x,int k,int n){if(x==0){int a=0,b=0,c=0,d=0;for(int j=0; j<k; ++j){if(ans[j]==1)a++;if(ans[j]==2)b++;if(ans[j]==4)c++;if(ans[j]==8)d++;}if(!vis[a][b][c][d]){vis[a][b][c][d]=true;for(int j=0; j<k; ++j)printf("%d ",ans[j]);printf("\n");ant++;}return ;}for(int i=1; i<=n; i*=2){if(x-i>=0){ans[k]=i;dfs(x-i,k+1,n);}}}int main(){freopen("xx.out","w",stdout);for(int i=1;i<=16;++i){ant=0;cout<<"n="<<i<<endl;memset(vis,0,sizeof(vis));memset(ans,0,sizeof(ans));dfs(i,0,i);cout<<"Result="<<ant<<endl;}// cout << "Hello world!" << endl;return 0;}
打表结果:
n=1 1 Result=1 n=2 1 1 2 Result=2 n=3 1 1 1 1 2 Result=2 n=4 1 1 1 1 1 1 2 2 2 4 Result=4 n=5 1 1 1 1 1 1 1 1 2 1 2 2 1 4 Result=4 n=6 1 1 1 1 1 1 1 1 1 1 2 1 1 2 2 1 1 4 2 2 2 2 4 Result=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 Result=6 n=8 1 1 1 1 1 1 1 1 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1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 2 2 1 1 1 1 1 1 1 1 1 1 1 1 4 1 1 1 1 1 1 1 1 1 1 2 2 2 1 1 1 1 1 1 1 1 1 1 2 4 1 1 1 1 1 1 1 1 2 2 2 2 1 1 1 1 1 1 1 1 2 2 4 1 1 1 1 1 1 1 1 4 4 1 1 1 1 1 1 1 1 8 1 1 1 1 1 1 2 2 2 2 2 1 1 1 1 1 1 2 2 2 4 1 1 1 1 1 1 2 4 4 1 1 1 1 1 1 2 8 1 1 1 1 2 2 2 2 2 2 1 1 1 1 2 2 2 2 4 1 1 1 1 2 2 4 4 1 1 1 1 2 2 8 1 1 1 1 4 4 4 1 1 1 1 4 8 1 1 2 2 2 2 2 2 2 1 1 2 2 2 2 2 4 1 1 2 2 2 4 4 1 1 2 2 2 8 1 1 2 4 4 4 1 1 2 4 8 2 2 2 2 2 2 2 2 2 2 2 2 2 2 4 2 2 2 2 4 4 2 2 2 2 8 2 2 4 4 4 2 2 4 8 4 4 4 4 4 4 8 8 8 16 Result=36可以发现规律了: 当n是奇数的时候,值就等于前面那个偶数的值.n为偶数的时候,它的值等于前面那个奇数的值再加上它本身/2的那个值AC code:#include<iostream>#include<cstdio>using namespace std;#define M 1000000typedef long long ll;const int mode=1000000000;int n;ll num[M]={0,1,2};void slove(){int i,j;for(i=3;i<=M;++i){if(i&1) num[i]=num[i-1]; //奇数就等于前一位的偶数else num[i]=(num[i-1]+num[i/2])%mode;//偶数等于前一位+当前数/2}}int main(){slove();while(scanf("%d",&n)!=EOF){//printf("%I64d\n",num);printf("%I64d\n",num%mode);//cout<<num<<endl;}return 0;}
相关文章推荐
- maven + redis + 分布式锁
- Hibernate---QBC查询(1)
- 扩展KMP模板
- mongodb index(索引)
- mysql读写分离和主从复制的基本原理
- HDU 4749 Parade Show 数据结构KMP变形
- 《墨子》 —— 兼爱与非攻
- Spark钨丝计划:让火花(Spark)更接近灯丝(Rare Metal)详解(2)
- VS2010把项目发布、打包成可安装部署的应用程序
- 线程的创建
- 编程思想之多线程与多进程(1)——以操作系统的角度述说线程与进程
- 进程间通信——消息队列
- Angular2 开发规范
- codeforces 701B B. Cells Not Under Attack(水题)
- linphone-LinphoneAuthInfo.java
- Brupsuite暴力破解DVWA渗透平台
- 类成员的3种访问属性 类的3种继承方式(继承性也叫派生性)
- 线程上下文切换和进程上下文切换的区别
- 使用PUTTY连接虚拟机Ubuntu16.04
- POJ:3641 Pseudoprime numbers(快速幂)