题目

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.

输入格式

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.

输出格式

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

输入样例

5

1 30000

2 30000

3 30000

4 30000

5 30000

输出样例

1

3

11

70

629

题解

题意:

用n种颜色染n个点的环,问有多少本质不同的染法

Polya定理##

我们设置换群为G,\(c(i)\)表示置换i的循环节个数,m为色数,L为答案

则\(L = \frac{1}{\mid G \mid} \sum_{i=1}^{s} m^{c(i)}\)

本题有n个置换,置换i循环节个数为\(gcd(n,i)\)

那么我们有:

\(ans = \frac{1}{n} \sum_{i=1}^{n} n^{gcd(n,i)}\)

\(\qquad = \frac{1}{n} \sum_{d|n} n^d \sum_{i=1}^{n}[gcd(n,i)==1]\)

\(\qquad = \sum_{d|n} n^{d-1} \sum_{i=1}^{n/d}[gcd(n/d,i)==1]\)

\(\qquad = \sum_{d|n} n^{d-1} \phi(n/d)\)

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long int
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define BUG(s,n) for (int i = 1; i <= (n); i++) cout<<s[i]<<' '; puts("");
using namespace std;
const int maxn = 100005,maxm = 100005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57) {if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57) {out = (out << 3) + (out << 1) + c - '0'; c = getchar();}
return out * flag;
}
int P,prime[maxn],primei;
bool isn[maxn];
void init(){
for (int i = 2; i < maxn; i++){
if (!isn[i]) prime[++primei] = i;
for (int j = 1; j <= primei && i * prime[j] < maxn; j++){
isn[i * prime[j]] = true;
if (i % prime[j] == 0) break;
}
}
}
int qpow(int a,int b){
int ans = 1;
for (; b; b >>= 1,a = (LL)a * a % P)
if (b & 1) ans = (LL)ans * a % P;
return ans;
}
int phi(int n){
int ans = n;
for (int i = 1; prime[i] * prime[i] <= n; i++){
int p = prime[i];
if (n % p == 0){
ans = ans - ans / p;
while (n % p == 0) n /= p;
}
}
if (n > 1) ans = ans - ans / n;
return ans % P;
}
int cal(int n,int d){
return qpow(n,d - 1) * phi(n / d) % P;
}
int main(){
init();
int T = read(),n,ans;
while (T--){
n = read(); P = read(); ans = 0;
for (int i = 1; i * i <= n; i++){
if (n % i == 0){
ans = (ans + cal(n,i)) % P;
if (i * i != n) ans = (ans + cal(n,n / i)) % P;
}
}
printf("%d\n",ans);
}
return 0;
}

POJ2154 Color 【Polya定理 + 欧拉函数】的更多相关文章

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

    题目描述 $T$ 组询问,用 $n$ 种颜色去染 $n$ 个点的环,旋转后相同视为同构.求不同构的环的个数模 $p$ 的结果. $T\le 3500,n\le 10^9,p\le 30000$ . 题 ...

  2. POJ2154 Color【 polya定理+欧拉函数优化】(三个例题)

    由于这是第一天去实现polya题,所以由易到难,先来个铺垫题(假设读者是看过课件的,不然可能会对有些“显然”的地方会看不懂): 一:POJ1286 Necklace of Beads :有三种颜色,问 ...

  3. poj2154Color polya定理+欧拉函数优化

    没想到贱贱的数据居然是错的..搞得我调了一中午+晚上一小时(哦不d飞LJH掉RP毕竟他是BUFF)结果重判就对了五次.. 回归正题,这题傻子都看得出是polya定理(如果你不是傻子就看这里),还没有翻 ...

  4. poj2154(polya定理+欧拉函数)

    题目链接:http://poj.org/problem?id=2154 题意:n 种颜色的珠子构成一个长为 n 的环,每种颜色珠子个数无限,也不一定要用上所有颜色,旋转可以得到状态只算一种,问有多少种 ...

  5. poj 2154 Color【polya定理+欧拉函数】

    根据polya定理,答案应该是 \[ \frac{1}{n}\sum_{i=1}^{n}n^{gcd(i,n)} \] 但是这个显然不能直接求,因为n是1e9级别的,所以推一波式子: \[ \frac ...

  6. 【POJ2154】Color Pólya定理+欧拉函数

    [POJ2154]Color 题意:求用$n$种颜色染$n$个珠子的项链的方案数.在旋转后相同的方案算作一种.答案对$P$取模. 询问次数$\le 3500$,$n\le 10^9,P\le 3000 ...

  7. Luogu4980 【模板】Polya定理(Polya定理+欧拉函数)

    对于置换0→i,1→i+1……,其中包含0的循环的元素个数显然是n/gcd(i,n),由对称性,循环节个数即为gcd(i,n). 那么要求的即为Σngcd(i,n)/n(i=0~n-1,也即1~n). ...

  8. poj 2154 Color(polya计数 + 欧拉函数优化)

    http://poj.org/problem?id=2154 大致题意:由n个珠子,n种颜色,组成一个项链.要求不同的项链数目.旋转后一样的属于同一种.结果模p. n个珠子应该有n种旋转置换.每种置换 ...

  9. poj2409 & 2154 polya计数+欧拉函数优化

    这两个题都是项链珠子的染色问题 也是polya定理的最基本和最经典的应用之一 题目大意: 用m种颜色染n个珠子构成的项链,问最终形成的等价类有多少种 项链是一个环.通过旋转或者镜像对称都可以得到置换 ...

随机推荐

  1. 用fast rcnn绘制loss曲线遇到的问题

    运行fast rcnn的train,会进入ipython,要先exit退出才能继续运行程序 绘制图像时,用了命令: ./tools/train_net.py --gpu 0 --solver mode ...

  2. python_51_函数返回值1

    def test1(): '只执行return以前的' print('test1返回值为0 ') return 0 print('这句不会被执行的') x=test1() print(x)#值为0 d ...

  3. ReentrantReadWriteLock的使用

    ReentrantReadWriteLock的规则是: 多线程情况下:读-写互斥.写-读互斥.写-写互斥.读-读共享 验证“读-写互斥.写-读互斥.写-写互斥.读-读共享” //单个线程 读-读 不互 ...

  4. python3安装pip

    wget --no-check-certificate https://pypi.python.org/packages/source/p/pip/pip-8.0.2.tar.gz#md5=3a73c ...

  5. JDBC 基本语法总结

    实现JDBC操作: 静态SQL执行 ① 注册驱动 Class.forName("com.mysql.jdbc.Driver"); ② 创建连接 Connection con = D ...

  6. c++question 004 c++基本数据类型有哪些?

    (1)signed int类型 整数型 占内存4个字节 一个字节byte 占8个二进制位 一个整型就占32位 (2)short int  短整型 占内存2个字节 一个短整型占16位 (3)long i ...

  7. Tomcat启动xxx.keystore文件找不到

    在server.xml里配置了 <Connector SSLEnabled="true" acceptCount="1000000" clientAuth ...

  8. 二十五、MySQL 索引

    MySQL 索引 MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度. 打个比方,如果合理的设计且使用索引的MySQL是一辆兰博基尼的话,那么没有设计和使用索 ...

  9. [转载]win10(64bit)上安装MySQL-python

    https://blog.csdn.net/builder_taoge/article/details/78292302 https://blog.csdn.net/qq_26808915/artic ...

  10. Postgres安装详解

    PG安装 一.基础包的安装(yum源的配置,可以采用光盘挂载,及ftp yum源,针对外网环境忽略此步): yum -y install wget tcpdump glibc libgcc gcc g ...