Sumsets POJ - 2229(计数dp)
題意:
給一個(gè)數(shù),是集合的總數(shù)和,集合元素只能為2的次冪數(shù),問(wèn)這樣的集合有多少?
題目:
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
分析:
計(jì)數(shù)dp,dp[i]定義為i有多少種分解方案,
(1)對(duì)一個(gè)數(shù) iii,首先如果它是奇數(shù),則它的方案總數(shù)與 i?1i-1i?1的方案總數(shù)相同,因?yàn)橹恍枰?i?1i-1i?1的每個(gè)分解方案集合里面多一個(gè)元素 1即可.
(2)如果它是偶數(shù),則 i/2i/2i/2的每個(gè)分解方案的每個(gè)數(shù)*2即可得到 iii,這樣得到的 iii的分解方案是不存在 1的(因?yàn)?*2所以至少為 2),因此還要考慮有 1的時(shí)候 iii的分解方案有多少種,可以這樣想,先忽略一個(gè)1,考慮剩下的數(shù)任意組合的方法數(shù),也就是 i?1i-1i?1的方案數(shù),最后再加上一個(gè) 1即可歸為有 1的時(shí)候i的分解方案數(shù),下面我會(huì)作圖解答:
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; }備戰(zhàn)ccpc分站賽ing ,題目分析簡(jiǎn)略,見(jiàn)諒,轉(zhuǎn)載請(qǐng)注明出處。。。。。
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的Sumsets POJ - 2229(计数dp)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 减肥可以吃的零食有哪些
- 下一篇: GCD and LCM Aizu - 0