poj2154-color-polyan次二面体+欧拉函数优化
N<=1e9,O(nlogn)的做法会超时。从枚举置换转变为枚举轮换长度,然后可以利用欧拉函数,把复杂度变为O(√n * logn)
/*--------------------------------------------------------------------------------------*/ #include <algorithm>
#include <iostream>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <cmath>
#include <set>
#include <map> //debug function for a N*M array
#define debug_map(N,M,G) printf("\n");for(int i=0;i<(N);i++)\
{for(int j=;j<(M);j++){\
printf("%d",G[i][j]);}printf("\n");}
//debug function for int,float,double,etc.
#define debug_var(X) cout<<#X"="<<X<<endl;
#define LL long long
const int INF = 0x3f3f3f3f;
const LL LLINF = 0x3f3f3f3f3f3f3f3f;
/*--------------------------------------------------------------------------------------*/
using namespace std; int N,M,T;
int MOD = 1e9+; int Eular(int n)
{
int ans = n;
for(int i=;i*i<=n;i++)
{
if(n%i == )
{
ans -= ans/i;
while(n%i == ) n/= i;
}
}
if(n>) ans -= ans/n;
return ans;
}
int pow_mod(int x,int cnt)
{
int base = x%MOD,res = ;
while(cnt)
{
if(cnt&) {res *= base;res %= MOD;}
base *= base;base %= MOD;
cnt >>= ;
}
return res;
} int polya(int n,int m)
{
int res = ;
int i;
for(i=;i*i<n;i++)
{
if(n%i == )
{
res += Eular(i)%MOD * pow_mod(m,n/i-)%MOD;res %= MOD;
res += Eular(n/i)%MOD * pow_mod(m,i-)%MOD;res %= MOD;
}
}
if(i*i == n) res += Eular(i)%MOD*pow_mod(m,i-)%MOD;
return res%MOD;
} int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&N,&MOD);
printf("%d\n",polya(N,N));
}
}
poj2154-color-polyan次二面体+欧拉函数优化的更多相关文章
- POJ2154 Color【 polya定理+欧拉函数优化】(三个例题)
由于这是第一天去实现polya题,所以由易到难,先来个铺垫题(假设读者是看过课件的,不然可能会对有些“显然”的地方会看不懂): 一:POJ1286 Necklace of Beads :有三种颜色,问 ...
- poj 2154 Color(polya计数 + 欧拉函数优化)
http://poj.org/problem?id=2154 大致题意:由n个珠子,n种颜色,组成一个项链.要求不同的项链数目.旋转后一样的属于同一种.结果模p. n个珠子应该有n种旋转置换.每种置换 ...
- POJ2154 Color 【Polya定理 + 欧拉函数】
题目 Beads of N colors are connected together into a circular necklace of N beads (N<=1000000000). ...
- poj2409 & 2154 polya计数+欧拉函数优化
这两个题都是项链珠子的染色问题 也是polya定理的最基本和最经典的应用之一 题目大意: 用m种颜色染n个珠子构成的项链,问最终形成的等价类有多少种 项链是一个环.通过旋转或者镜像对称都可以得到置换 ...
- poj2154Color polya定理+欧拉函数优化
没想到贱贱的数据居然是错的..搞得我调了一中午+晚上一小时(哦不d飞LJH掉RP毕竟他是BUFF)结果重判就对了五次.. 回归正题,这题傻子都看得出是polya定理(如果你不是傻子就看这里),还没有翻 ...
- poj 2154 Color 欧拉函数优化的ploya计数
枚举位移肯定超时,对于一个位移i.我们须要的是它的循环个数,也就是gcd(i,n),gcd(i,n)个数肯定不会非常多,由于等价于n的约数的个数. 所以我们枚举n的约数.对于一个约数k,也就是循环个数 ...
- poj 2154 Color【polya定理+欧拉函数】
根据polya定理,答案应该是 \[ \frac{1}{n}\sum_{i=1}^{n}n^{gcd(i,n)} \] 但是这个显然不能直接求,因为n是1e9级别的,所以推一波式子: \[ \frac ...
- 【poj2154】Color Polya定理+欧拉函数
题目描述 $T$ 组询问,用 $n$ 种颜色去染 $n$ 个点的环,旋转后相同视为同构.求不同构的环的个数模 $p$ 的结果. $T\le 3500,n\le 10^9,p\le 30000$ . 题 ...
- UVA12493 - Stars(求1-N与N互质的个数)欧拉函数
Sample Input 3 4 5 18 36 360 2147483647 Sample Output 1 1 2 3 6 48 1073741823 题目链接:https://uva.onlin ...
随机推荐
- centos性能监控系列三:监控工具atop详解
引言 Linux以其稳定性,越来越多地被用作服务器的操作系统(当然,有人会较真地说一句:Linux只是操作系统内核:).但使用了Linux作为底层的操作系统,是否我们就能保证我们的服务做到7*24地稳 ...
- Effective Java 59 Avoid unnecessary use of checked exceptions
The burden is justified if the exceptional condition cannot be prevented by proper use of the API an ...
- 深入理解spring中的各种注解
Spring中的注解大概可以分为两大类: 1)spring的bean容器相关的注解,或者说bean工厂相关的注解: 2)springmvc相关的注解. spring的bean容器相关的注解,先后有:@ ...
- P2P行业专业术语(最全)
网贷:P2P网络借款,指个体与个体之间通过网站实现借贷,中间网站称为网贷平台,简称平台. 角色类 投资者:在网站上把钱借出去的用户,年龄必须是18周岁以上具有中国国籍的公民. 借款者(贷款者):是指有 ...
- 数据结构--AC自动机--hdu 2896
病毒侵袭 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- oracle创建数据库和用户
以前开发的时候用得比较多的是mysql和sql server,oracle用的比较少,用起来比较生疏,mysql和sql server用起来比较类似,就oracle的使用方式和他们不同,oracle在 ...
- hdu Flow Problem (最大流 裸题)
最大流裸题,贴下模版 view code#include <iostream> #include <cstdio> #include <cstring> #incl ...
- POj3104 Drying(二分)
Drying Time Limit: 2000MS Memory Limit: 65536K Description It is very hard to wash and especially to ...
- redis 非集群的主从配置及切换
单纯的master-slave不能称之为集群,只能叫做读写分离.此案例只针对master为单点服务,且程序端写死master为可写,slave为只读.若master宕机则不可用,若主从未开启持久化,不 ...
- 在vscode中使用Git
用了git最方便的就是比如在公司写了很多代码后回到家打开vscode只需要点击一下pull就能全部同步过来.是不是很方便....毕竟之前我都是拿u盘拷贝回家或者存到云盘再下载下来.. 我这里用的是国内 ...