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
题目大意是: 给你一个数字,然后你要用2的倍数(1 2 4 8 。。。。)组合出这个数 问总共的组合数方法 (MOD 1000000000)
这个 有个很容易想到的方法,dp[i][j]表示 在用到2的i次方的数的情况下,组合出j的总共的方法数
然后转移方程就是 dp[i][j] = Sogima k=(0,20) (dp[i-1][j-1<<k])
以下是这种方法的代码
//
// Create by Running Photon on 2015-03-05
//
题目大意是: 给你一个数字,然后你要用2的倍数(1 2 4 8 。。。。)组合出这个数 问总共的组合数方法 (MOD 1000000000)
这个 有个很容易想到的方法,dp[i][j]表示 在用到2的i次方的数的情况下,组合出j的总共的方法数
然后转移方程就是 dp[i][j] = Sogima k=(0,20) (dp[i-1][j-1<<k])
以下是这种方法的代码
//
// Create by Running Photon on 2015-03-05
//
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <algorithm>
#include <cctype>
#include <stack>
#include <queue>
#include <map>
#include <string>
#include <set>
#include <vector>
using namespace std;
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <algorithm>
#include <cctype>
#include <stack>
#include <queue>
#include <map>
#include <string>
#include <set>
#include <vector>
using namespace std;
#define CLR(x) memset(x,0,sizeof x)
#define ll long long
const int inf=0x3f3f3f3f;
const int maxn=1e6+5;
const ll MOD=1e9;
int n;
int dp[maxn];
int c[20];
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
#endif
CLR(dp);
dp[0] = c[0] = 1;
for(int i = 1; i <= 20; i++) c[i] = c[i-1] << 1;
for(int i = 0; i <= 20 && c[i] <= 1000000; i++) for(int j = c[i]; j <= 1000000; j++){
dp[j] = (dp[j] + dp[j - c[i]]) % MOD;
}
ios_base::sync_with_stdio(0);
while(scanf("%d", &n)!=EOF){
printf("%d\n",dp[n]);
}
return 0;
}
然后擦着时间过得
这道题还有递推的方法,我们这么考虑
当n是奇数的时候 必然有一个多出的1 无论怎么都无法组合出2
那么就有 dp[i] = dp[i-1]
当n是偶数的时候 我们可以假设组成n的元素的值全部都是大于等于2的 @1
那么问题规模就缩减了
那么dp[i] = dp[i/2] + dp[i-1]
dp[i-1]肯定会有多出来的一个1 那么里面的内容必然与 @1中说到的互斥
所以转移方程成立 (dp[i-1]是可以包含所有有1存在的情况的 与@1恰好互补的
主要代码如下:
#define ll long long
const int inf=0x3f3f3f3f;
const int maxn=1e6+5;
const ll MOD=1e9;
int n;
int dp[maxn];
int c[20];
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
#endif
CLR(dp);
dp[0] = c[0] = 1;
for(int i = 1; i <= 20; i++) c[i] = c[i-1] << 1;
for(int i = 0; i <= 20 && c[i] <= 1000000; i++) for(int j = c[i]; j <= 1000000; j++){
dp[j] = (dp[j] + dp[j - c[i]]) % MOD;
}
ios_base::sync_with_stdio(0);
while(scanf("%d", &n)!=EOF){
printf("%d\n",dp[n]);
}
return 0;
}
然后擦着时间过得
这道题还有递推的方法,我们这么考虑
当n是奇数的时候 必然有一个多出的1 无论怎么都无法组合出2
那么就有 dp[i] = dp[i-1]
当n是偶数的时候 我们可以假设组成n的元素的值全部都是大于等于2的 @1
那么问题规模就缩减了
那么dp[i] = dp[i/2] + dp[i-1]
dp[i-1]肯定会有多出来的一个1 那么里面的内容必然与 @1中说到的互斥
所以转移方程成立 (dp[i-1]是可以包含所有有1存在的情况的 与@1恰好互补的
主要代码如下:
CLR(dp);
dp[0] = 1;
for(int i = 1; i <= 1000000; i++){
if(i & 1) dp[i] = dp[i-1];
else dp[i] = (dp[i-1] + dp[i / 2]) % MOD;
}
return 0;
}