Time Limit:10 Sec Memory limit:128 MB
submit:779 solved:456
[Submit] [Status] [Discuss] Description
"Set theory and Graph theory" This course has a homework problem, ask the students to find {1, 2, 3, 4, 5} All meet the following conditions of the subset: If X is in this subset, 2x and 3x cannot be in the sub-set. The students do not like the problem with the enumeration nature, so it becomes the following question: For any positive integer n≤100000, how to find out {1, 2,..., n} The number of the subset of the constraints to meet the above constraint (just output the results of 1,000,000,001 modulo), Now this problem is yours.
Input
Only one row, where there is a positive integer n,30% the data satisfies n≤20.
Output
Contains only a positive integer representing the number of subsets of {1, 2,..., n} that meet the above constraints.
Sample Input
4Sample Output8
"Sample Interpretation"
There are 8 sets that meet the requirements, namely the empty set, {1},{1,4},{2},{2,3},{3},{3,4},{4}.
HINT Source
Day2
[Submit] [Status] [Discuss]
Exercises
The key to this question is whether you can see that you want to construct a matrix, assuming that there is an element x in the set, The matrix is:
1x 3x 9x 27x ...
2x 6x 18x 54x ...
4x 12x 36x 108x ...
According to the range of N, found that there are up to 11 columns, 18 rows, in which we select some number, can not select adjacent, with the pressure DP to find the total number of scenarios , but 5 did not appear, the same 5 multiples also did not appear, 7 did not appear, for less than N, There is a good majority does not appear, so should record which number appeared, did not appear as the first element of the matrix, again constructs the matrix, finally multiplies several matrices the scheme number, the attention takes the modulus.
However, I have been this problem many times, is tle, because I put f[][] array into 100*3000, so every time the structure of the matrix is emptied will call Memset (f,0,sizeof (0)); The complexity of the memset is related to the size of the array, so timeouts ...
1#include <iostream>2#include <cstdio>3#include <cstdlib>4#include <cstring>5#include <algorithm>6#include <cmath>7#include <queue>8#include <vector>9 using namespacestd;TentypedefLong LongLL; One ConstLL mod=1000000001; A ConstLL maxn=100010; -LL n,ans=1, b[ -][ the]; -LL f[ -][2048];//F[i][j] Indicates the number of feasible scenarios for line I with State J theLL state[ -]; - BOOLVIS[MAXN]; -InlinevoidCalc (LL x) { -memset (b,0,sizeof(b)); Memset (F,0,sizeof(f)); memset (state,0,sizeof(state)); + //Construct a matrix -b[1][1]=x; + for(LL i=2; i<= One; i++){ A if(b[1][i-1]*3<=n) b[1][i]=b[1][i-1]*3; at Elseb[1][i]=n+1;//more than n directly with n+1, so as not to explode long long - } - for(LL i=2; i<= -; i++){ - for(LL j=1; j<= One; j + +){ - if(b[i-1][j]*2<=n) b[i][j]=b[i-1][j]*2; - Elseb[i][j]=n+1; in } - } to /*Select the possible states of each row*/ + for(intI=1; i<= -; i++){ - for(intj=1; j<= One; j + +){ the if(b[i][j]<=N) { *state[i]+= (1<< (J-1));//according to the inverse symmetry $vis[b[i][j]]=1;Panax Notoginseng } - } the } +f[0][0]=1;//Line 0th, one is not selected, that is, the number of scenarios with a status of 0 is 1 A for(intI=1; i<= -; i++){ the for(intj=0; j<=state[i];j++){ + for(intk=0; k<=state[i-1];k++){ - if(f[i-1][k]&& (j&k) = =0&& (j& (j>>1))==0&& (j& (j<<1))==0){ $F[i][j]= (f[i][j]+f[i-1][K])%MOD; $ } - } - } the } -Ans= (ans*f[ -][0])%MOD;Wuyi } the intMain () { -scanf"%lld",&N); Wu for(LL i=1; i<=n;i++){ - if(vis[i]==false) calc (i); About } $printf"%lld", ANS); - return 0; -}
Bzoj 2734: [HNOI2012] Collection selection