Portal: Gift
Test instructions: A necklace of n (n<=1e9) pearls containing the lucky number (with and consisting only of 4 or 7), taking the numbers within the interval [l,r], the adjacent numbers cannot be the same, and the same sequence of rotations is one, and the final number of necklaces can be formed.
Analysis: This is one of the most comprehensive topics I have done (too slag), the first digital DP filter out the interval [l,r] The number of lucky numbers, Dp[pos] for non-restricted conditions and POS bit contains the number of lucky numbers, and then the memory of the search, casual (direct DFS does not know will not time out, I practice 900+ms insurance, should be directly DFS will time out), do not consider the rotation of the same situation, you can DP re-structure matrix, dp[i][0] to arrive at the first bit, the last bead differs from the initial bead, dp[i][1] means to reach the last bead of the first and the same, The state transition equation is:
dp[i][0]=dp[i-1][0]* (m-2) +dp[i-1][1]* (m-1) (M is the lucky number kind).
Dp[i][1]=dp[i-1][0]*1
The above constructs the matrix fast to calculate the total number after the sum of the Burnside Theorem + the tolerance to obtain the answer, this question is more common, may refer to the poj2888 procedure.
#include <stdio.h>#include<cstring>#include<cmath>#include<algorithm>#include<queue>#include<cstdlib>#defineLL Long Long#defineN 25#defineMoD 1000000007using namespacestd;/************************** Matrix fast Power **************************/structmatrix{LL m[2][2]; voidinit () { for(intI=0;i<2; i++) for(intj=0;j<2; j + +) M[i][j]= (i==j); }}m;matrix mult (Matrix A,matrix b) {matrix C; memset (C.M,0,sizeof(C.M)); for(intk=0;k<2; k++) for(intI=0;i<2; i++) { if(a.m[i][k]==0)Continue; for(intj=0;j<2; j + +) {C.m[i][j]= (C.m[i][j]+a.m[i][k]*b.m[k][j])%MoD; } } returnC;} Matrix Quick_pow (Matrix A,intN) {Matrix res; Res.init (); while(n) {if(n&1) res=mult (res,a); A=mult (a,a); N>>=1; } returnRes;} LL Calc (ll n,ll m) {matrix res; res.m[0][0]=m-2; res.m[0][1]=m-1; res.m[1][0]=1; res.m[1][1]=0; Res=quick_pow (res,n-1); //printf ("M=%lld n=%lld res=%lld\n", m,n,res.m[0][1]); returnm*res.m[0][1]%MoD;}/************************** Matrix fast Power **************************/ll POW (ll a,ll N) {ll res=1; A%=MoD; while(n) {if(n&1) res=res*a%MoD; A=a*a%MoD; N>>=1; } returnRes;}/********************** **************************/#defineMAXN 12#defineMAXM 32000#defineEPS 1e-8BOOLP[maxm];vector<int>Prime;vector<int>Factor;vector<int>Primefactor;voidInit () {intI, J; Prime.clear (); Memset (P,true,sizeof(p)); for(i =2; I < the; i++) { if(P[i]) { for(j = i * i; j < maxm; J + =i) p[j]=false; } } for(i =2; i < MAXM; i++) { if(P[i]) prime.push_back (i); }}voidPrime (intx) {intI, TMP; Primefactor.clear (); TMP= (int) (sqrt (Double) x) +EPS); for(i =0; Prime[i] <= tmp; i++) { if(x% prime[i] = =0) {primefactor.push_back (prime[i]); while(x% prime[i] = =0) x/=Prime[i]; } } if(X >1) Primefactor.push_back (x);}intMul (intXint&k) {intI, ans; Ans=1; for(i = k =0; X X >>=1, i++) { if(X &1) {k++; Ans*=Primefactor[i]; } } returnans;} LL Phi (intx) {intI, J, T, ans, TMP; Prime (x); Ans=0; T= (int) primefactor.size (); for(i =1; I < (1<< t); i++) {tmp=Mul (i, j); if(J &1) ans+ = x/tmp; Elseans-= x/tmp; } return(X-ans)%MoD;}/********************** **************************/ll solve (ll n,ll m) {ll ans=0; for(intI=1; i*i<=n;i++) { if(n%i==0) { inta=i,b=n/i; if(i*i==N) {ans= (Ans+phi (n/a) *calc (a,m))%MoD; } Else{ans= (Ans+phi (n/a) *calc (a,m))%MoD; Ans= (Ans+phi (n/b) *calc (b,m))%MoD; }}} ans=ans*pow (n,mod-2)%MoD; returnans;}/************************ Digit dp**********************/LL dig[ -],dp[ -]; LL DFS (intPosintFlagintLimitintFzore) { if(pos==0)returnFlag; if(!fzore&&!limit&&~dp[pos])returnDp[pos]; intLen=limit?dig[pos]:9; LL ans=0; for(intI=0; i<=len;i++) { if(i==4|| i==7|| i==0){ if(fzore&&i==0) Ans+=dfs (pos-1,0, I==len&&limit,1); Else if(i==4|| i==7) Ans+=dfs (pos-1,1, I==len&&limit,0);} } if(!fzore&&!limit) dp[pos]=ans; returnans;} ll Fun (ll x) {intlen=0; if(x<4)return 0; while(x) {dig[++len]=x%Ten; X/=Ten; } returnDFS (Len,0,1,1);}/************************ Digit dp**********************/intMain () {LL n,l,r; Init (); Memset (DP,-1,sizeof(DP)); while(SCANF ("%lld%lld%lld", &n,&l,&r) >0) {LL num=fun (R)-fun (l1); if(n==0) {puts ("0");Continue; } if(n==1) {printf ("%d\n", num%MoD); Continue; } printf ("%d\n", Solve (n,num)); }}View Code
HUST 1569 (Burnside theorem + tolerant + digital dp+ matrix fast Power)