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 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定理:

假设$G$是$p$个对象的一个置换群,用$m$种颜色涂染$p$个对象,则不同颜色的方案数为

$L = \frac{1}{|G|}\sum_{g_i \in G}m^{c(g_i)}$

$G = \{g_1, g_2, \dots g_s \}$,$c(g_i)$为置换$g_i$的循环节数

本题而言第$i$种置换的循环节数为$gcd(n, i)$

因此答案为$L = \frac{1}{n}\sum_{i = 1}^n n^{gcd(i, n}$

枚举约数,用欧拉函数计算,时间复杂度$O(T\sqrt(N) f(n))$,$f(n)$表示小于$\sqrt(n)$的质因子的个数

#include<cstdio>
#include<algorithm>
#include<cmath>
#include<map>
#define LL long long
const int MAXN = 1e5 + ;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
int T, N, mod;
int fastpow(int a, int p, int mod) {
int base = ; a %= mod;
while(p) {
if(p & ) base = (base * a) % mod;
a = (a * a) % mod; p >>= ;
}
return base % mod;
}
int prime[MAXN], tot, vis[MAXN];
void Prime() {
for(int i = ; i <= MAXN - ; i++) {
if(!vis[i]) prime[++tot] = i;
for(int j = ; j <= tot && prime[j] * i <= MAXN - ; j++) {
vis[i * prime[j]] = ;
if(!i % prime[j]) break;
}
}
}
int phi(int x, int mod) {
int limit , ans = x;
for(int i = ; i <= tot && prime[i] * prime[i] <= x; i++) {
if(!(x % prime[i])) {
ans = ans - ans / prime[i];
while((x % prime[i]) == ) x /= prime[i];
}
}
if(x > ) ans = ans - ans / x;
// printf("%d", ans % mod);
return ans % mod;
}
main() {
T = read();
Prime();
while(T--) {
N = read(); mod = read();
int ans = , now = N;
for(int d = ; d * d<= N; d++) {
if(d * d == N)
ans = (ans + fastpow(N, d - , mod) % mod * phi(N / d, mod) % mod) % mod;
else if( (N % d) == ) {
ans = (ans + fastpow(N, d - , mod) * phi(N / d, mod) + fastpow(N, N / d - , mod) * phi(d, mod)) % mod;
} //printf("%d\n", ans);
}
//if(now > 0) ans += fastpow(N, now - 1, mod) * phi(N / now, mod);
printf("%d\n", ans % mod);
}
}

POJ2154 Color(Polya定理)的更多相关文章

  1. poj2154 Color ——Polya定理

    题目:http://poj.org/problem?id=2154 今天学了个高端的东西,Polya定理... 此题就是模板,然而还是写了好久好久... 具体看这个博客吧:https://blog.c ...

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

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

  3. [POJ1286&POJ2154&POJ2409]Polya定理

    Polya定理 L=1/|G|*(m^c(p1)+m^c(p2)+...+m^c(pk)) G为置换群大小 m为颜色数量 c(pi)表示第i个置换的循环节数 如置换(123)(45)(6)其循环节数为 ...

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

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

  5. POJ2154 Color 【Polya定理 + 欧拉函数】

    题目 Beads of N colors are connected together into a circular necklace of N beads (N<=1000000000). ...

  6. BZOJ 1815: [Shoi2006]color 有色图(Polya定理)

    题意 如果一张无向完全图(完全图就是任意两个不同的顶点之间有且仅有一条边相连)的每条边都被染成了一种颜色,我们就称这种图为有色图. 如果两张有色图有相同数量的顶点,而且经过某种顶点编号的重排,能够使得 ...

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

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

  8. 置换群和Burnside引理,Polya定理

    定义简化版: 置换,就是一个1~n的排列,是一个1~n排列对1~n的映射 置换群,所有的置换的集合. 经常会遇到求本质不同的构造,如旋转不同构,翻转交换不同构等. 不动点:一个置换中,置换后和置换前没 ...

  9. 百练_2409 Let it Bead(Polya定理)

    描述 "Let it Bead" company is located upstairs at 700 Cannery Row in Monterey, CA. As you ca ...

随机推荐

  1. Nginx + Keepalived 实例(测试可行)

    Nginx_Master: 192.168.1.103 提供负载均衡 Nginx_BackUp: 192.168.1.104 负载均衡备机 Nginx_VIP_TP: 192.168.1.108 网站 ...

  2. 亲测SQLServer的最大连接数

    很多做架构设计.程序开发.运维.技术管理的朋友可能或多或少有这样的困惑: SQLServer到底支持多少连接数的并发? SQLServer是否可以满足现有的应用吗? 现有的技术架构支持多少连接数的并发 ...

  3. GitKraken使用教程-基础部分(2)

    3. 修改用户名 为了方便项目中代码的管理,需要重新编辑用户名. 点击右上角的图像即可看到如下图 3‑1所示的下拉菜单,鼠标悬于Profile上,会出现一个Edit按钮. 图 3‑1 编辑个人信息 点 ...

  4. JAVA SE collection接口

    collection接口:{Set,List,Queue} Set:无序集合,元素不可重复          List:有序集合,元素可重复          Queue:队列 Set{EnumSet ...

  5. (一)Hybrid app混合开发模式

    hybrid app是什么? 这里我们先看一下词条上的定义 Hybrid App:Hybrid App is a mobile application that is coded in both br ...

  6. ios上【点击select元素,输入框自动获得焦点的问题】

    今天遇到了一个很奇怪的问题:在ios手机上,如果页面出现滚动条,点击select元素的时候,偶尔会出现 “ 页面上的某一个输入框自动获得焦点 “ 的问题. 这个问题困扰我好久,一直找不到答案,今天终于 ...

  7. CSS透明度设置(兼容性)

    一句话搞定透明背景! .transparent_class { filter:alpha(opacity=50); -moz-opacity:0.5; -khtml-opacity: 0.5; opa ...

  8. [topcoder]SRM 647 DIV 2

    第一题,送分题. 第二题, #include <vector> #include <algorithm> #include <map> #include <q ...

  9. MongoDB 安装笔记

    一.MongoDB的安装 1.在MongoDB的官网下载对应的安装文件() 2.解压安装文件 #解压tgz文件 tar -zxvf mongodb-linux-x86_64-ubuntu1604-3. ...

  10. TP5.1:模板赋值与变量输出

    模板赋值:assign() 模板渲染:fetch() 前提准备: 1.在app/index/controller下建立一个控制器,名为Templates.php,里面有test1和test2方法,并且 ...