# BZOJ1677: [Usaco2005 Jan]Sumsets 求和

## 1677: [Usaco2005 Jan]Sumsets 求和

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 570  Solved: 310
[Submit][Status]

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

一个整数N.

7

6

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

## Source

 1 #include<cstdio>
2 #include<cstdlib>
3 #include<cmath>
4 #include<cstring>
5 #include<algorithm>
6 #include<iostream>
7 #include<vector>
8 #include<map>
9 #include<set>
10 #include<queue>
11 #include<string>
12 #define inf 1000000000
13 #define maxn 1000000+1000
14 #define maxm 500+100
15 #define eps 1e-10
16 #define ll long long
17 #define pa pair<int,int>
18 #define mod 1000000000
19 using namespace std;
21 {
22     int x=0,f=1;char ch=getchar();
23     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
24     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
25     return x*f;
26 }
27 int n;
28 ll f[maxn];
29 int main()
30 {
31     freopen("input.txt","r",stdin);
32     freopen("output.txt","w",stdout);
34     f[0]=1;
35     for(int i=0;i<=30;i++)
36     {
37         int x=1<<i;
38         if(x>n)break;
39         for(int j=x;j<=n;j++)f[j]+=f[j-x],f[j]%=mod;
40     }
41     printf("%lld\n",f[n]);
42     return 0;
43 }
View Code

UPD:

posted @ 2014-08-28 13:54  ZYF-ZYF  Views(156)  Comments(0Edit  收藏  举报