polya定理主要是解决那些什么手镯,项链旋转翻转的问题
先要知道它旋转翻转一共有多少种情况,最后要除一下
翻转简单,主要是旋转,旋转就是找有多少个循环
一个循环,就是你旋转之后,这些循环里的珠子互相置换
所以对于同一个循环,颜色应该一样
如果有x个循环,m个颜色,那么方案就是xm
对于旋转i格,那么循环个数就是gcd(i,n)
如果n特别大,那么就不能枚举i了,就要枚举因子,然后还要求欧拉函数
对于有限制的情况,如果明确给出了限制,那么直接矩阵快速幂优化,因为一般这样颜色不会多
如果不给出明确限制,就说相邻的颜色不一样,然后颜色又很多的话,就要推公式了
推完公式然后继续矩阵快速幂优化
poj 2154
裸题,就是要求欧拉函数的,并且n个珠子n个颜色,最后不能乘n的逆元,因为不互质,所以前面要除掉n
代码:
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
using namespace std;
#define MAX 40005
#define MAXN 1000005
#define maxnode 15
#define sigma_size 30
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lrt rt<<1
#define rrt rt<<1|1
#define middle int m=(r+l)>>1
#define LL long long
#define ull unsigned long long
#define mem(x,v) memset(x,v,sizeof(x))
#define lowbit(x) (x&-x)
#define pii pair<int,int>
#define bits(a) __builtin_popcount(a)
#define mk make_pair
#define limit 10000
//const int prime = 999983;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f;
const double pi = acos(-1.0);
const double inf = 1e18;
const double eps = 1e-8;
const LL mod = 998244353;
const ull mx = 133333331;
/*****************************************************/
inline void RI(int &x) {
char c;
while((c=getchar())<'0' || c>'9');
x=c-'0';
while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0';
}
/*****************************************************/
bool prime[MAX];
int pr[MAX];
int tot;
void init(){
mem(prime,0);
tot=0;
for(int i=2;i<MAX;i++){
if(!prime[i]){
for(int j=i*i;j<MAX;j+=i) prime[j]=1;
pr[tot++]=i;
}
}
}
int p;
int qpow(int a,int n){
int ans=1;
while(n){
if(n&1) ans=ans*a%p;
a=a*a%p;
n>>=1;
}
return ans;
}
int phi(int x){
int ans=1;
for(int i=0;i<tot&&pr[i]*pr[i]<=x;i++){
if(x%pr[i]==0){
ans*=(pr[i]-1);
x/=pr[i];
while(x%pr[i]==0){
x/=pr[i];
ans*=pr[i];
}
}
}
if(x!=1) ans*=(x-1);
return ans%p;
}
int main(){
int t;
cin>>t;
init();
while(t--){
int n;
cin>>n>>p;
int ans=0;
for(int i=1;i*i<=n;i++){
if(n%i==0){
ans=(ans+qpow(n%p,i-1)*phi(n/i)%p)%p;
if(n/i!=i) ans=(ans+qpow(n%p,n/i-1)*phi(i)%p)%p;
}
}
cout<<ans<<endl;
}
return 0;
}
poj 2888
有限制条件,颜色少,直接矩阵,然后特判n=1的时候是m,循环个数是x,矩阵快速幂x次,从第一个到第x+1个,因为第一个和x+1个都是一个循环的开头,所以要同色,所以,直接加同色的就行,并且能保证x和x+1满足条件限制。所以这样算的话n=1的时候要特判呢
代码:
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
using namespace std;
#define MAX 40005
#define MAXN 1000005
#define maxnode 15
#define sigma_size 30
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lrt rt<<1
#define rrt rt<<1|1
#define middle int m=(r+l)>>1
#define LL long long
#define ull unsigned long long
#define mem(x,v) memset(x,v,sizeof(x))
#define lowbit(x) (x&-x)
#define pii pair<int,int>
#define bits(a) __builtin_popcount(a)
#define mk make_pair
#define limit 10000
//const int prime = 999983;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f;
const double pi = acos(-1.0);
const double inf = 1e18;
const double eps = 1e-8;
const LL mod = 9973;
const ull mx = 133333331;
/*****************************************************/
inline void RI(int &x) {
char c;
while((c=getchar())<'0' || c>'9');
x=c-'0';
while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0';
}
/*****************************************************/
bool prime[MAX];
int pr[MAX];
int tot;
void init(){
mem(prime,0);
tot=0;
for(int i=2;i<MAX;i++){
if(!prime[i]){
for(int j=i*i;j<MAX;j+=i) prime[j]=1;
pr[tot++]=i;
}
}
}
struct Matrix{
int n;
LL maze[maxnode][maxnode];
void init(int n){
this->n=n;
mem(maze,0);
}
Matrix operator * (Matrix &rhs){
Matrix m;
m.init(n);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
for(int k=0;k<n;k++){
m.maze[i][j]=m.maze[i][j]+maze[i][k]*rhs.maze[k][j];
}
m.maze[i][j]%=mod;
}
}
return m;
}
}A;
LL qpow(Matrix a,LL n){
Matrix ans;
ans.init(a.n);
for(int i=0;i<ans.n;i++) ans.maze[i][i]=1;
while(n){
if(n&1) ans=ans*a;
a=a*a;
n>>=1;
}
LL sum=0;
for(int i=0;i<ans.n;i++) sum=(sum+ans.maze[i][i])%mod;
return sum;
}
LL phi(int x){
int ans=1;
for(int i=0;i<tot&&pr[i]*pr[i]<=x;i++){
if(x%pr[i]==0){
x/=pr[i];
ans*=(pr[i]-1);
while(x%pr[i]==0){
x/=pr[i];
ans*=pr[i];
}
}
}
if(x!=1) ans*=(x-1);
return ans%mod;
}
LL cal(int x){
return qpow(A,x);
}
LL ppow(LL a,LL n){
LL ans=1;
while(n){
if(n&1) ans=ans*a%mod;
a=a*a%mod;
n>>=1;
}
return ans;
}
int main(){
int t;
cin>>t;
init();
while(t--){
int n,m,k;
cin>>n>>m>>k;
A.init(m);
for(int i=0;i<m;i++){
for(int j=0;j<m;j++) A.maze[i][j]=1;
}
for(int i=0;i<k;i++){
int a,b;
scanf("%d%d",&a,&b);
A.maze[a-1][b-1]=0;
A.maze[b-1][a-1]=0;
}
if(n==1){
cout<<m<<endl;
continue;
}
LL ans=0;
for(int i=1;i*i<=n;i++){
if(n%i==0){
ans=(ans+cal(i)*phi(n/i)%mod)%mod;
if(n/i!=i) ans=(ans+cal(n/i)*phi(i)%mod)%mod;
}
}
cout<<ans*ppow(n,mod-2)%mod<<endl;
}
return 0;
}
hdu 2865
限制条件是相邻的不同色,颜色特别大,不能直接矩阵,所以可以直接推导公式,推完之后再矩阵,循环个数x,要保证x和第一个不同色。
别忘了中间那个珠子
代码:
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
using namespace std;
#define MAX 40005
#define MAXN 1000005
#define maxnode 3
#define sigma_size 30
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lrt rt<<1
#define rrt rt<<1|1
#define middle int m=(r+l)>>1
#define LL long long
#define ull unsigned long long
#define mem(x,v) memset(x,v,sizeof(x))
#define lowbit(x) (x&-x)
#define pii pair<int,int>
#define bits(a) __builtin_popcount(a)
#define mk make_pair
#define limit 10000
//const int prime = 999983;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f;
const double pi = acos(-1.0);
const double inf = 1e18;
const double eps = 1e-8;
const LL mod = 1e9+7;
const ull mx = 133333331;
/*****************************************************/
inline void RI(int &x) {
char c;
while((c=getchar())<'0' || c>'9');
x=c-'0';
while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0';
}
/*****************************************************/
bool prime[MAX];
int pr[MAX];
int tot;
void init(){
mem(prime,0);
tot=0;
for(int i=2;i<MAX;i++){
if(!prime[i]){
for(int j=i*i;j<MAX;j+=i) prime[j]=1;
pr[tot++]=i;
}
}
}
struct Matrix{
int n;
LL maze[maxnode][maxnode];
void init(int n){
this->n=n;
mem(maze,0);
}
Matrix operator * (Matrix &rhs){
Matrix m;
m.init(n);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
for(int k=0;k<n;k++)
m.maze[i][j]=(m.maze[i][j]+maze[i][k]*rhs.maze[k][j])%mod;
}
}
return m;
}
}A;
LL k;
LL qpow(Matrix a,LL n){
Matrix ans;
ans.init(a.n);
for(int i=0;i<ans.n;i++) ans.maze[i][i]=1;
while(n){
if(n&1) ans=ans*a;
a=a*a;
n>>=1;
}
LL f3=(k*(k-1)%mod)*(k-2)%mod;
LL f2=k*(k-1)%mod;
return (ans.maze[0][0]*f3+ans.maze[0][1]*f2)%mod;
}
LL phi(int x){
int ans=1;
for(int i=0;i<tot&&pr[i]*pr[i]<=x;i++){
if(x%pr[i]==0){
x/=pr[i];
ans*=(pr[i]-1);
while(x%pr[i]==0){
x/=pr[i];
ans*=pr[i];
}
}
}
if(x!=1) ans*=(x-1);
return ans%mod;
}
LL cal(int x){
if(x==1) return 0;
if(x==2) return k*(k-1)%mod;
return qpow(A,x-3);
}
LL ppow(LL a,LL n){
LL ans=1;
while(n){
if(n&1) ans=ans*a%mod;
a=a*a%mod;
n>>=1;
}
return ans;
}
int main(){
init();
int n;
while(cin>>n>>k){
A.init(2);
k=k-1;
A.maze[0][0]=k-2;A.maze[0][1]=k-1;
A.maze[1][0]=1;A.maze[1][1]=0;
LL ans=0;
for(int i=1;i*i<=n;i++){
if(n%i==0){
ans=(ans+cal(i)*phi(n/i)%mod)%mod;
if(n/i!=i) ans=(ans+cal(n/i)*phi(i)%mod)%mod;
}
}
cout<<(ans*ppow(n,mod-2)%mod)*(k+1)%mod<<endl;
}
return 0;
}