Sumsets POJ - 2229(计数dp)
題意:
給一個數,是集合的總數和,集合元素只能為2的次冪數,問這樣的集合有多少?
題目:
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:
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
分析:
計數dp,dp[i]定義為i有多少種分解方案,
(1)對一個數 iii,首先如果它是奇數,則它的方案總數與 i?1i-1i?1的方案總數相同,因為只需要在 i?1i-1i?1的每個分解方案集合里面多一個元素 1即可.
(2)如果它是偶數,則 i/2i/2i/2的每個分解方案的每個數*2即可得到 iii,這樣得到的 iii的分解方案是不存在 1的(因為 *2所以至少為 2),因此還要考慮有 1的時候 iii的分解方案有多少種,可以這樣想,先忽略一個1,考慮剩下的數任意組合的方法數,也就是 i?1i-1i?1的方案數,最后再加上一個 1即可歸為有 1的時候i的分解方案數,下面我會作圖解答:
dp[i]dp[i]dp[i]=iii &1 ? dp[i?1]:(dp[i?2]+dp[i/2])dp[i-1]:(dp[i-2]+dp[i/2])dp[i?1]:(dp[i?2]+dp[i/2])%mod ;
初始化 dp[0]=dp[1]=1dp[0]=dp[1]=1dp[0]=dp[1]=1.遞推就可以了.
AC模板:
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int M=1e6+10; const int mod=1e9; typedef long long ll; int n; ll dp[M]; int main() {scanf("%d",&n);dp[0]=dp[1]=1;for(int i=2;i<=n;i++)dp[i]=i&1?dp[i-1]:(dp[i-1]+dp[i/2])%mod;printf("%lld\n",dp[n]);return 0; }備戰ccpc分站賽ing ,題目分析簡略,見諒,轉載請注明出處。。。。。
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的Sumsets POJ - 2229(计数dp)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 减肥可以吃的零食有哪些
- 下一篇: 糙米热量高为什么可以减肥