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 ...
随机推荐
- Atitit. 软件设计 模式 变量 方法 命名最佳实践 vp820 attilax总结命名表大全
Atitit. 软件设计 模式 变量 方法 命名最佳实践 vp820 attilax总结命名表大全 1. #====提升抽象层次1 2. #----使用通用单词1 3. #===使用术语..1 4. ...
- 数据库update的异常一例
调查一列bug,偶然发现了update的一个特性:update t set a=a+1 where id=4; 这样一条简单的语句,也会发生让人意外的事情: 如果 a 的初始值为null时,无论你up ...
- 我的vim配置文件.vimrc
我的vim配置文件.vimrc map <silent> <F10> :TlistToggle<cr>map <silent> <F8> : ...
- Openstack-Ceilometer-SNMP的使用
1. 物理服务器配置 1.1安装 #yum install -y net-snmp net-snmp-utils 1.2 配置 复制[附件]中snmpd.conf文件到/etc/snmp/目 ...
- 【Ext.Net学习笔记】06:Ext.Net GridPanel的用法(GridPanel 折叠/展开行、GridPanel Selection、 可编辑的GridPanel)
GridPanel 折叠/展开行 Ext.Net GridPanel的行支持折叠/展开功能,这个功能个人觉得还说很有用处的,尤其是数据中包含图片等内容的时候. 下面来看看效果: 使用行折叠/展开功能之 ...
- 【读书笔记《Android游戏编程之从零开始》】5.Android 游戏开发常用的系统控件(ProgressBar、Seekbar)
3.7 ProgressBar ProgressBar类官方文档地址:http://developer.android.com/reference/android/widget/ProgressBar ...
- codeforces 484B B. Maximum Value(二分)
题目链接: B. Maximum Value time limit per test 1 second memory limit per test 256 megabytes input standa ...
- UVA 12377 Number Coding --DFS
题意:给一串数字,第一个数是Num的话,要使后面的数字组成Num个数,而且为不降的,将这Num个数分配到9个素因子上作为指数,问能组成多少个不同的数 解法:dfs一遍,看后面的数字能组成Num个不降数 ...
- 第2章 面向对象的设计原则(SOLID):6_开闭原则
6. 开闭原则(Open Closed Principle,OCP) 6.1 定义 (1)一个类应该对扩展开放,对修改关闭.要求通过扩展来实现变化,而且是在不修改己有的代码情况下进行扩展,也不必改动己 ...
- TestLink学习三:发送邮件的两种配置方法
第一种:修改config.inc.php中的[smtp],配置为默认本地发送,用hotmail用户做接收,调试成功!(本人未尝试这种) // ----------------------------- ...