The main idea: a string of rings consisting of n ring, for each ring, there is a total of M points, R color, and the same ring after the rotation of the same, and then a string necklace is composed of n rings, at the same time to meet the adjacent two rings can not be the same, this string necklace on a position inserted a special thing, And if special things are inserted in different places, even if the rings are the same, the two necklaces are different, ask for a total number of different love necklace.
Idea: First can find out a total number of different rings, and because there is that special thing, the equivalent of the string necklace even after the same, but the special thing inserted in the position is certainly different, so that is not considered rotation, only consider the next location of the different love necklace scheme number.
Make ans indicate how many different rings there are.
Then you can use the repulsion principle. For the I ring and the i+1 ring are the same, can be regarded as the first I limit, then the nth limit is the nth ring and the 1th limit is different, that is required to meet all the constraints of the number of scenarios. So that Si is a set of the number of schemes that satisfy the limit of I, that is, the sum of the S1~SN, that is, its complement, then the total scheme number minus all that does not satisfy any one of the limits plus any number of the two limit.
Then what is this formula, first of all the total scheme number is obviously ans^n, and then not meet any one limit number is two adjacent to the same, can be seen as a side join two points the same, that is, C (n,1) *ans^ (n-1), and so on, then you can get: Sigma (0<=i <=n, ( -1) ^i*c (n,i) *ans^ (n-i)), and then found that this is the two-term theorem, you can get (ans-1) ^n, but is it really? When I=n, there will be a bug, the formula is calculated by the answer is ( -1) ^n, however, n constraints are not satisfied with the situation that all colors are the same, there are ans species, so also add (n&1?1-m:m-1).
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <cmath>6 using namespacestd;7 #defineP 32145678 #defineMAXN 2000209 Ten intM,r,tot; One intPRIME[MAXN],PHI[MAXN]; A BOOLISPRIME[MAXN]; - Long LongN; - the intPowerintALong Longk) { - if(k==0)return 1; - if(k==1)returna%p; - intX=power (a,k/2), ans=1ll*x*x%p; + if(k&1) ans=1ll*a*ans%p; - returnans; + } A at intFphi (intx) { - intans=x; - for(intI=2; i*i<=x;i++) - if(x%i==0){ -ans=ans-ans/i; - while(x%i==0) x/=i; in } - if(x!=1) ans=ans-ans/x; to returnans; + } - the intMain () { *scanf"%lld%d%d", &n,&m,&r); tot=0, Memset (IsPrime,1,sizeof(IsPrime)), phi[1]=1; $ for(intI=2; i<maxn;i++){Panax Notoginseng if(Isprime[i]) prime[++tot]=i,phi[i]=i-1; - for(intj=1; J<=tot && i*prime[j]<maxn;j++){ theisprime[i*prime[j]]=0; + if(i%prime[j]==0){ Aphi[i*prime[j]]=phi[i]*Prime[j]; the Break; + } -phi[i*prime[j]]=phi[i]* (prime[j]-1); $ } $ } - intans=0; - for(intI=1; i*i<=m;i++) the if(m%i==0){ -Ans= (Ans+1ll*power (r,i) *fphi (m/i)%p)%p;Wuyi if(i*i!=m) ans= (Ans+1ll*power (r,m/i) *phi[i]%p)%p; the } -Ans=1ll*ans*power (m,p-2)%p; Wu intT= (1ll*power (ans-1, n) + (n&1?1-ans:ans-1))%p; -printf"%d\n", (t+p)%p); About return 0; $}
View Code
bzoj4330:jsoi2012 Necklace of Love