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
一道递推规律题,也可以用DP来做
递推思路:
1.若n是奇数,发现和为n的每种排列都是在和为n-1的每种排列前加上一个1,所以a[n]=a[n-1]
2.n为偶数,可以通过考虑和为n的每种排列里有没有1(只有有或没有两种情况)
A.如果有,那么至少2个1,对于这些排列,如果把这两个1去掉正好就是n-2的所有排列
B.如果没有,对于这些排列,如果把每个加数都除以2,正好就是n/2的所有排列
有个不错题解:http://www.2cto.com/kf/201412/364159.html
代码:
#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;
#define pi acos(-1.0)
#define inf 0x7fffffff
#define M 1005
ll a[1000002];
int main()
{
int n,i;
a[1]=1; a[2]=2; a[3]=2;
a[4]=4;
scanf("%d",&n);
for(i=5;i<=n;i++)
{
if(i%2==1) a[i]=a[i-1];
else
{
a[i]=(a[i-2]+a[i/2])%1000000000;
}
}
printf("%d",a[n]);
}