题目

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. AOSP常见漏洞类型简介

    Heap/Stack Overflow(CVE-2017-0541) 漏洞出现在PushcdlStack函数中,如下所示   # /external/sonivox/arm-wt-22k/lib_sr ...

  2. windows下编辑器Emacs的安装与配置

    一年成为Emacs高手(像神一样使用编辑器) http://blog.csdn.net/redguardtoo/article/details/7222501   原创作品,允许转载,转载时请务必以超 ...

  3. es6中的变量声明

    目录 es6中的变量声明 变量的声明 es6中的变量声明 变量的声明 for (var i = 0; i < 5; i++) { console.log(i) } var声明 作用域问题 上面的 ...

  4. centos 7 安装以及ip配置

    1.安装: root 200M: swap 内存的2倍,如果内存超过4g,最大设为8g就够了: / 剩余: 2.ip配置 (1)动态配置:dhclient ip add (2)静态配置:vi /etc ...

  5. js判断是否是大小写,数字等方法

    function isEmail(str){ var regu = "^(([0-9a-zA-Z]+)|([0-9a-zA-Z]+[_.0-9a-zA-Z-]*))@([a-zA-Z0-9- ...

  6. 【PHP】PHP中的排序函数sort、asort、rsort、krsort、ksort区别分析

    php编程中有时候会需要用上排序,在这里简单地整理一下集中sort的区别,方便查询 sort() 函数用于对数组单元从低到高进行排序. rsort() 函数用于对数组单元从高到低进行排序. asort ...

  7. PHP使用CURL_MULTI实现多线程采集

    $connomains = array( "http://www.baidu.com/", "http://www.hao123.com/", "ht ...

  8. 查询集 QuerySet和管理器Manager

    查询集 QuerySet 查询集,也称查询结果集.QuerySet,表示从数据库中获取的对象集合. 当调用如下过滤器方法时,Django会返回查询集(而不是简单的列表): all():返回所有数据. ...

  9. Python学习笔记(七)加密加盐

    MD5加密和加盐 Python的MD5加密 Python的hashlib模块的MD5加密,是比较简单一种加密,md5函数必须传入编译后的结果,否则会报错: Traceback (most recent ...

  10. Python9-条件-定时器-队列-day40

    复习 线程 线程是进程中的执行单位 线程是cpu执行的最小单位 线程之间资源共享 线程的开启和关闭以及切换的时间开销远远小于进程 线程本身可以在同一时间使用多个cpu,python与线程 由于cpyt ...