题目

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. 3218: 字符串字符统计—C语言

    3218: 字符串字符统计—C语言 时间限制: 1 Sec  内存限制: 128 MB提交: 270  解决: 129[提交][状态][讨论版][命题人:smallgyy] 题目描述 编写一函数,由实 ...

  2. 2018.6.5 Oracle plsql编程 游标的使用

    --3.查询10部门所有员工的姓名.(ref游标实现) 动态游标 declare --创建一种游标类型 type type_cursor is ref cursor; --声明变量指定游标类型 v_c ...

  3. ubuntu or centos 网卡无法启动

    [root@seasoned-bro:/home/daeh0f]# /etc/init.d/network restart Restarting network (via systemctl): Jo ...

  4. Bootstrap 弹出框(Popover)插件

    Bootstrap 弹出框(Popover)插件与Bootstrap 提示工具(Tooltip)插件类似,提供了一个扩展的视图,用户只需要把鼠标指针悬停到元素上面即可.弹出框的内容完全由Bootstr ...

  5. 0.5px的边框

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 3_HA介绍和安装部署

    一.hadoop 2.x产生背景 1.hadoop 1.x中hdfs和mr在高可用和扩展性等方面存在问题.2.hdfs存在的问题:NN单点故障,难以应用于在线场景:NN压力过大,内存受限,影响系统扩展 ...

  7. eclipse中的字体大小设置和背景色设置

    1.字体大小设置 在basic下选择最后一个TextFont 护眼背景色设置 添加到自定义颜色后点确定 最后一步点apply

  8. css中让元素隐藏的多种方法

    { display: none; /* 不占据空间,无法点击 / } { visibility: hidden; / 占据空间,无法点击 / } { position: absolute; top: ...

  9. MySQL - EXISTS 和 NOT EXISTS

    语法规则:   SELECT * FROM tableName t WHERE 1 = 1 AND 2 = 2 AND EXISTS (SELECT * FROM tableName t2 WHERE ...

  10. PHP 作用域