POJ2154 Color 【Polya定理 + 欧拉函数】
题目
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定理 + 欧拉函数】的更多相关文章
- 【poj2154】Color Polya定理+欧拉函数
题目描述 $T$ 组询问,用 $n$ 种颜色去染 $n$ 个点的环,旋转后相同视为同构.求不同构的环的个数模 $p$ 的结果. $T\le 3500,n\le 10^9,p\le 30000$ . 题 ...
- POJ2154 Color【 polya定理+欧拉函数优化】(三个例题)
由于这是第一天去实现polya题,所以由易到难,先来个铺垫题(假设读者是看过课件的,不然可能会对有些“显然”的地方会看不懂): 一:POJ1286 Necklace of Beads :有三种颜色,问 ...
- poj2154Color polya定理+欧拉函数优化
没想到贱贱的数据居然是错的..搞得我调了一中午+晚上一小时(哦不d飞LJH掉RP毕竟他是BUFF)结果重判就对了五次.. 回归正题,这题傻子都看得出是polya定理(如果你不是傻子就看这里),还没有翻 ...
- poj2154(polya定理+欧拉函数)
题目链接:http://poj.org/problem?id=2154 题意:n 种颜色的珠子构成一个长为 n 的环,每种颜色珠子个数无限,也不一定要用上所有颜色,旋转可以得到状态只算一种,问有多少种 ...
- poj 2154 Color【polya定理+欧拉函数】
根据polya定理,答案应该是 \[ \frac{1}{n}\sum_{i=1}^{n}n^{gcd(i,n)} \] 但是这个显然不能直接求,因为n是1e9级别的,所以推一波式子: \[ \frac ...
- 【POJ2154】Color Pólya定理+欧拉函数
[POJ2154]Color 题意:求用$n$种颜色染$n$个珠子的项链的方案数.在旋转后相同的方案算作一种.答案对$P$取模. 询问次数$\le 3500$,$n\le 10^9,P\le 3000 ...
- 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). ...
- poj 2154 Color(polya计数 + 欧拉函数优化)
http://poj.org/problem?id=2154 大致题意:由n个珠子,n种颜色,组成一个项链.要求不同的项链数目.旋转后一样的属于同一种.结果模p. n个珠子应该有n种旋转置换.每种置换 ...
- poj2409 & 2154 polya计数+欧拉函数优化
这两个题都是项链珠子的染色问题 也是polya定理的最基本和最经典的应用之一 题目大意: 用m种颜色染n个珠子构成的项链,问最终形成的等价类有多少种 项链是一个环.通过旋转或者镜像对称都可以得到置换 ...
随机推荐
- 第七章 动态创建HTML内容
javascript也可以改变网页的结构和内容 document.write()方法 可以方便快捷地把字符串插入到文档里 document.write("<strong>hell ...
- pthread_cancel函数注意事项
/************************************************** 相关函数: #include <pthread.h> int pthread_can ...
- numpy学习(二)
ndarray的聚合操作 此博客讲的非常清楚,参照此博客即可 https://blog.csdn.net/qq_42571805/article/details/81146133
- mysql运维-二进制日志BINARY LOG清理
1.1 方法1:PURGE MASTER LOGS 语法: PURGE { BINARY | MASTER } LOGS { TO 'log_name' | BEFORE datetim ...
- 生产环境LAMP搭建 - 基于 fastcgi
生产环境LAMP搭建 - 基于 fastcgi 由于在module模式,php只是已http的模块形式存在,无形中加重了http的服务负载,通常在企业架构中,使用fastcgi的模式,将所有的服务都设 ...
- DeepFaceLab报错, Could not create cudnn handle 解决方法!
DeepFaceLab 虽然没有可视化界面,但是在众多换脸软件中,是安装最方便,更新最快,整体性能最佳的一个.这个软件对于系统依赖很低,也就是不需要装各种各样的“插件”. 但是即便如此,由于版本的不断 ...
- Java开发学生管理系统
Java 学生管理系统 使用JDBC了链接本地MySQL 数据库,因此在没有建立好数据库的情况下没法成功运行 (数据库部分, Java界面部分, JDBC部分) 资源下载: http://downlo ...
- Linux系统自启动脚
只需编辑/etc/init.d/rc.local文件,在最后加上你的脚本即可. 比如:我已经编写了一个脚本shell.sh,存放在/home/mars704/Desktop/ 下面在终端输入 gedi ...
- Android设为系统默认的短信应用
要设为系统默认的短信应用首先要配置一下AndroidManifest.xml文件,添加下列: <!-- BroadcastReceiver that listens for incoming S ...
- Android如何添加多张引导页
摘要:项目需要添加多张引导页,所以在网上搜集了一些资料并整理好. Step1 添加一个GuideActivity. 其实这个引导页无非就是一个Activity,里面有一个ViewPager而已.多张图 ...