POJ2154 Color

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 10322   Accepted: 3360

Description

Beads of N colors are connected together into a circular necklace of N beads (N<=1000000000). Your job is to calculate how many different kinds of the necklace can be produced. You should know that the necklace might not use up all the N colors, and the repetitions that are produced by rotation around the center of the circular necklace are all neglected.

You only need to output the answer module a given number P.

Input

The first line of the input is an integer X (X <= 3500) representing the number of test cases. The following X lines each contains two numbers N and P (1 <= N <= 1000000000, 1 <= P <= 30000), representing a test case.

Output

For each test case, output one line containing the answer.

Sample Input

5
1 30000
2 30000
3 30000
4 30000
5 30000

Sample Output

1
3
11
70
629

Source

POJ Monthly,Lou Tiancheng

数学问题 统计 polya原理

仍然是项链的套路,没有翻转只有旋转同构。

需要用到欧拉函数优化

1、数据范围大,要先打好素数筛

2、for统计答案的时候要一起算n和n/i,这样只用循环到sqrt(n)

没加这两个T飞了

 1 /*by SilverN*/
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<cmath>
 7 #include<vector>
 8 #define LL long long
 9 using namespace std;
10 const int mxn=100101;
11 int read(){
12     int x=0,f=1;char ch=getchar();
13     while(ch<‘0‘ || ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
14     while(ch>=‘0‘ && ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
15     return x*f;
16 }
17 int pri[mxn],cnt=0;
18 bool vis[mxn];
19 void init(){
20     for(int i=2;i<mxn;i++){
21         if(!vis[i])
22             pri[++cnt]=i;
23         for(int j=1;j<=cnt && (long long)pri[j]*i<mxn;j++){
24             vis[pri[j]*i]=1;
25             if(i%pri[j]==0)break;
26         }
27     }
28     return;
29 }
30 int phi(int x){
31     int res=x;
32     int m=sqrt(x+0.5);
33 //    for(int i=1;i<=cnt && x>1;i++){
34     for(int i=1;i<=cnt && pri[i]<=m;i++){
35         if(x%pri[i]==0){
36             res=res/pri[i]*(pri[i]-1);
37             while(x%pri[i]==0)
38                 x/=pri[i];
39         }
40     }
41 //    printf("x:%d %d\n",x,res);
42     if(x>1)res=res/x*(x-1);
43     return res;
44 }
45 int n,p;
46 int ksm(int c,int k){
47     int res=1;
48     while(k){
49         if(k&1)(res*=c)%=p;
50         c=c*c%p;
51         k>>=1;
52     }
53     return res;
54 }
55 int main(){
56     int i,j;
57     int T=read();
58     init();
59     while(T--){
60         n=read();p=read();
61         int ans=0;
62         for(i=1;i*i<n;i++){
63             if(n%i==0){
64 //                printf("i:%d phi:%d\n",n/i,phi(n/i));
65                 ans+=ksm(n%p,i-1)*(phi(n/i)%p);
66                 ans%=p;
67                 ans+=ksm(n%p,n/i-1)*(phi(i)%p);
68                 ans%=p;
69             }
70         }
71         if(i*i==n)ans=(ans+ksm(n%p,i-1)*(phi(i)%p))%p;
72         printf("%d\n",ans%p);
73     }
74     return 0;
75 }
时间: 2024-08-14 23:37:58

POJ2154 Color的相关文章

POJ2154 Color polya定理+欧拉定理

由于这是第一天去实现polya题,所以由易到难,先来个铺垫题(假设读者是看过课件的,不然可能会对有些“显然”的地方会看不懂): POJ1286 Necklace of Beads :有三种颜色,问可以翻转,可以旋转的染色方案数,n<24. 1,n比较小,恶意的揣测出题人很有可能出超级多组数据,所以先打表. 2,考虑旋转: for(i=0;i<n;i++) sum+=pow(n,gcd(n,i)); 3,考虑翻转: if(n&1) sum+=n*pow(3,n/2+1) ; else {

【Polya定理】【枚举约数】【欧拉函数】【Java】poj2154 Color

你随便写一下出来,发现polya原理的式子里面好多gcd是相同的,gcd(n,i)=k可以改写成gcd(n/k,i/k)=1,也就是说指数为k的项的个数为phi(n/k),就很好求了,最后除的那个n直接放到指数上即可,没必要用逆元. import java.util.*; import java.io.*; public class Main { public static int phi(int n){ int ans=n; for(int i=2;i*i<=n;++i){ if(n%i==0

POJ2154 Color【Polya定理】【欧拉函数】【整数快速幂】

题目链接: http://poj.org/problem?id=2154 题目大意: 给定 N 种颜色的珠子,每种颜色珠子的个数均不限,将这些珠子做成长度为 N 的项链. 问能做成多少种不重复的项链,最后结果对 P 取模.并且两条项链相同,当且仅当两条 项链通过旋转后能重合在一起,且对应珠子的颜色相同. 解题思路: Polya定理的应用.先来看Polya定理. Polya定理:设 G = {a1,a2,-,ag}是 N 个对象的置换群,用 M 种颜色给这 N 个 对象着色,则不同的着色 方案数为

POJ2154 Color(Polya定理)

Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11654   Accepted: 3756 Description Beads of N colors are connected together into a circular necklace of N beads (N<=1000000000). Your job is to calculate how many different kinds of the nec

HDU2481 Toy

Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 661    Accepted Submission(s): 363 Problem Description On birthday, Anthony got a toy. It is constructed with N+1(N>=3) balls and 2*N sticks. All

置换群和Burnside引理,Polya定理

定义简化版: 置换,就是一个1~n的排列,是一个1~n排列对1~n的映射 置换群,所有的置换的集合. 经常会遇到求本质不同的构造,如旋转不同构,翻转交换不同构等. 不动点:一个置换中,置换后和置换前没有区别的排列 Burnside引理:本质不同的方案数=每个置换下不动点的个数÷置换总数(一个平均值) Polya定理:一个置换下不动点的个数=颜色^环个数.(辅助Burnside引理,防止枚举不动点复杂度过高) 这篇文章写得很详细了(具体的在此不说了): Burnside引理与Polya定理 **特

【poj2154】 Color

http://poj.org/problem?id=2154 (题目链接) 题意 n个珠子的项链,可以染上n中颜色,项链可以旋转不能翻转,求染色方案数. Solution 经典的公式: \begin{aligned} ans &= \sum_{i=0}^{n-1} gcd(n,i)\\ &= \sum_{d|n} (n^{d-1}*φ(\frac{n}{d})) \end{aligned} 于是就可以求了,然而时限卡太死,只能用int,还必须枚举质数求phi.. 代码 // poj2154

【poj2154】Color Polya定理+欧拉函数

题目描述 $T$ 组询问,用 $n$ 种颜色去染 $n$ 个点的环,旋转后相同视为同构.求不同构的环的个数模 $p$ 的结果. $T\le 3500,n\le 10^9,p\le 30000$ . 题解 Polya定理+欧拉函数 根据 poj2409 中得到的结论,答案为: $\frac{\sum\limits_{i=1}^nn^{\gcd(i,n)}}n=\sum\limits_{i=1}^nn^{\gcd(i,n)-1}$ 由于 $n$ 有 $10^9$ 之大,因此考虑优化这个式子. 枚举

Color颜色对照表

Color.AliceBlue 240,248,255 Color.LightSalmon 255,160,122 Color.AntiqueWhite 250,235,215 Color.LightSeaGreen 32,178,170 Color.Aqua 0,255,255 Color.LightSkyBlue 135,206,250 Color.Aquamarine 127,255,212 Color.LightSlateGray 119,136,153 Color.Azure 240,