POJ2154 Color(Polya定理)
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 11654 | Accepted: 3756 |
Description
You only need to output the answer module a given number P.
Input
Output
Sample Input
5
1 30000
2 30000
3 30000
4 30000
5 30000
Sample Output
1
3
11
70
629
Source
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定理)的更多相关文章
- poj2154 Color ——Polya定理
题目:http://poj.org/problem?id=2154 今天学了个高端的东西,Polya定理... 此题就是模板,然而还是写了好久好久... 具体看这个博客吧:https://blog.c ...
- 【poj2154】Color Polya定理+欧拉函数
题目描述 $T$ 组询问,用 $n$ 种颜色去染 $n$ 个点的环,旋转后相同视为同构.求不同构的环的个数模 $p$ 的结果. $T\le 3500,n\le 10^9,p\le 30000$ . 题 ...
- [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)其循环节数为 ...
- POJ2154 Color【 polya定理+欧拉函数优化】(三个例题)
由于这是第一天去实现polya题,所以由易到难,先来个铺垫题(假设读者是看过课件的,不然可能会对有些“显然”的地方会看不懂): 一:POJ1286 Necklace of Beads :有三种颜色,问 ...
- POJ2154 Color 【Polya定理 + 欧拉函数】
题目 Beads of N colors are connected together into a circular necklace of N beads (N<=1000000000). ...
- BZOJ 1815: [Shoi2006]color 有色图(Polya定理)
题意 如果一张无向完全图(完全图就是任意两个不同的顶点之间有且仅有一条边相连)的每条边都被染成了一种颜色,我们就称这种图为有色图. 如果两张有色图有相同数量的顶点,而且经过某种顶点编号的重排,能够使得 ...
- poj 2154 Color【polya定理+欧拉函数】
根据polya定理,答案应该是 \[ \frac{1}{n}\sum_{i=1}^{n}n^{gcd(i,n)} \] 但是这个显然不能直接求,因为n是1e9级别的,所以推一波式子: \[ \frac ...
- 置换群和Burnside引理,Polya定理
定义简化版: 置换,就是一个1~n的排列,是一个1~n排列对1~n的映射 置换群,所有的置换的集合. 经常会遇到求本质不同的构造,如旋转不同构,翻转交换不同构等. 不动点:一个置换中,置换后和置换前没 ...
- 百练_2409 Let it Bead(Polya定理)
描述 "Let it Bead" company is located upstairs at 700 Cannery Row in Monterey, CA. As you ca ...
随机推荐
- python 自动安装工具 setuptools(easy_install) 的使用
1.下载安装 python 安装工具,下载地址:http://pypi.python.org/pypi/setuptools ,可以找到正确的版本进行下载. 2.解压缩后双击 ez_setup.py ...
- (转)Shell脚本编程--Uniq命令
uniq 原文:http://blog.csdn.net/xifeijian/article/details/9209627 uniq命令可以去除排序过的文件中的重复行,因此uniq经常和sort合用 ...
- git如何使用
Git是分布式的,但多数时候仍然要使用中央仓库作为所有开发者的交互中心,和svn一样,开发人员仍要在本地写代码并提交到中央服务器.Git相较于svn最大的优势就在于其强大的分支系统,而git的工作流程 ...
- Fluent API配置
1.Fluent API配置Model试用行更广 2.使用方法: public class TransferConfig: EntityTypeConfiguration<TransferInf ...
- JDK基本知识
JDK发展史: JDK1.0:提供了一个纯解释的Java虚拟机实现 JDK1.3:把Java技术体系拆分为3个方向,J2SE,J2EE,J2ME,并且Java虚拟机第一次内置了JIT JDK1.4:增 ...
- autofac学习
Instance Scope 1.instance per dependency (the default) builder.RegisterType<classes>();等价于 ...
- JS的函数参数传递为值传递
function setAge(i) { alert(i);//24 i = 18; alert(i);//18,i的改变不会影响外面的age }; var age = 24; setAge(age) ...
- Android学习笔记4——Activity详解
在 Android 开发过程中,与程序员打交道最多的应该就是作为四大组件之一的 Activity 了.接下来我们就一起来揭开 Activity 的神秘面纱吧~ 一.概述 什么是 Activity(活动 ...
- Java—封装
封装 将类的某些信息隐藏在类的内部,不允许外部程序直接访问,而是通过该类提供的方法类实现对隐藏信息的操作和访问. 封装的实现步骤:修改属性的可见性(设为private)=>创建setter和ge ...
- [topcoder]SRM 647 DIV 2
第一题,送分题. 第二题, #include <vector> #include <algorithm> #include <map> #include <q ...