题意:用2的幂次的数相加构成N,求总方案数%1e9
思路:如果n是奇数,那么所有方案里面一定都包含有1,所以只需dp[n-1]的所有方案增加1就可以;如果n是偶数的话,那么分2种情况:方案里面含有1的方案可由dp[n-2]的方案+1+1构成。方案里面不含有1的可由dp[n/2]的方案*2构成,dp[n]=dp[n-2]+dp[n/2]。
代码:
1 #include <iostream> 2 #include <queue> 3 #include <stack> 4 #include <cstdio> 5 #include <vector> 6 #include <map> 7 #include <set> 8 #include <bitset> 9 #include <algorithm> 10 #include <cmath> 11 #include <cstring> 12 #include <cstdlib> 13 #include <string> 14 #include <sstream> 15 #include <time.h> 16 #define x first 17 #define y second 18 #define pb push_back 19 #define mp make_pair 20 #define lson l,m,rt*2 21 #define rson m+1,r,rt*2+1 22 #define mt(A,B) memset(A,B,sizeof(A)) 23 #define lowbit(x) (x&(-x)) 24 using namespace std; 25 typedef long long LL; 26 typedef unsigned long long ull; 27 const double PI = acos(-1); 28 const int N=1e6+10; 29 //const int M=1e6+10; 30 const LL mod=1e9; 31 const int inf = 0x3f3f3f3f; 32 const LL INF=0x3f3f3f3f3f3f3f3fLL; 33 const double esp=1e-10; 34 LL dp[N]; 35 int main() 36 { 37 #ifdef Local 38 freopen("data.h","r",stdin); 39 #endif 40 ios::sync_with_stdio(false); 41 cin.tie(0); 42 int n; 43 dp[1]=1;dp[2]=2; 44 for(int i=3;i<N;i++) 45 { 46 if(i%2==0)dp[i]=(dp[i-2]+dp[i/2])%mod; 47 else dp[i]=dp[i-1]%mod; 48 } 49 while(cin>>n) 50 { 51 cout<<dp[n]<<endl; 52 } 53 return 0; 54 #ifdef Local 55 cerr << "time: " << (LL) clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl; 56 #endif 57 }