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. 安卓压力测试之monkey

    步骤: 1.把要测试的apk包放在 SDK-platfrom-tools下 2.配置adb.exe的环境变量 3.手机连接上电脑(虚拟机和真机只能连接一个) 4.运行:adb devices   查看 ...

  2. 使用 Charles 获取 https 的数据

    1. 配置 Charles 根证书 首先打开 Charles: 然后如下图操作: 之后会弹出钥匙串,如果不弹出,请自行打开钥匙串,如下图: 系统默认是不信任 Charles 的证书的,此时对证书右键, ...

  3. Day3监督学习——决策树原理

    Day3 机器学习监督学习——决策树原理 一.决策树的原理 1.机器学习中分类和预测算法的评估: 准确率 速度 强壮型:有数据缺失或错误时算法的运行 可规模性:数量级规模比较大 可解释性 2.决策树( ...

  4. LeetCode 704.二分查找(C++)

    给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target  ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1. 示例 1: 输入: num ...

  5. Deferred跟promise跟js同步问题记录

    之前的时候,碰到过几次同事问我,说js的同步怎么处理,就是我想先执行这段代码(耗时相对较长的一行,多数是异步的一些api调用),执行完了之后我再执行下边这句,每次我都很无奈的说不晓得,如果是ajax的 ...

  6. Spring Boot(一)Hello World

    Spring Boot适合与微服务,方便快速开发,简化配置(约定>配置). 准备工作: SpringBoot 2.0 jdk8 IDEA或者eclipse+Spring Tool Suits 创 ...

  7. 【PHP】 hash加密

    之前对密码进行加密是在JS文件里做的,但是不行,改到PHP文件里进行加密比较安全,于是乎…… [原JS加密代码] var password = sha256_digest(pwd); 后来在网上调查方 ...

  8. Apache-ant安装以及环境变量配置、验证

    (一)安装 ant 下载地址: http://ant.apache.org/     根据自己电脑下载对应版本 下载完成以后,可自行解压到自己常用的盘中,但是要记住解压到哪里了,以便后续的环境变量配置 ...

  9. Visual Studio 要求导入 pfx 密钥以及导入后依然要求导入的解决办法

    本文为个人博客备份文章,原文地址: http://validvoid.net/visual-studio-pfx-import/ 导入密钥 在使用 Visual Studio 生产项目时,使用 pfx ...

  10. Spring课程 Spring入门篇 1-3Spring框架

    课程链接: 1 框架与类库的区别: 框架封装了逻辑,高内聚,类库是松散的工具组合 框架专注于某一个领域,类库通用性较强 2 为什么使用框架: a 业务系统日趋复杂 b 重用度高,开发效率和质量提高 c ...