POJ2154 Color
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 10322 | Accepted: 3360 |
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原理
仍然是项链的套路,没有翻转只有旋转同构。
需要用到欧拉函数优化
1、数据范围大,要先打好素数筛
2、for统计答案的时候要一起算n和n/i,这样只用循环到sqrt(n)
没加这两个T飞了
/*by SilverN*/
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#define LL long long
using namespace std;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int pri[mxn],cnt=;
bool vis[mxn];
void init(){
for(int i=;i<mxn;i++){
if(!vis[i])
pri[++cnt]=i;
for(int j=;j<=cnt && (long long)pri[j]*i<mxn;j++){
vis[pri[j]*i]=;
if(i%pri[j]==)break;
}
}
return;
}
int phi(int x){
int res=x;
int m=sqrt(x+0.5);
// for(int i=1;i<=cnt && x>1;i++){
for(int i=;i<=cnt && pri[i]<=m;i++){
if(x%pri[i]==){
res=res/pri[i]*(pri[i]-);
while(x%pri[i]==)
x/=pri[i];
}
}
// printf("x:%d %d\n",x,res);
if(x>)res=res/x*(x-);
return res;
}
int n,p;
int ksm(int c,int k){
int res=;
while(k){
if(k&)(res*=c)%=p;
c=c*c%p;
k>>=;
}
return res;
}
int main(){
int i,j;
int T=read();
init();
while(T--){
n=read();p=read();
int ans=;
for(i=;i*i<n;i++){
if(n%i==){
// printf("i:%d phi:%d\n",n/i,phi(n/i));
ans+=ksm(n%p,i-)*(phi(n/i)%p);
ans%=p;
ans+=ksm(n%p,n/i-)*(phi(i)%p);
ans%=p;
}
}
if(i*i==n)ans=(ans+ksm(n%p,i-)*(phi(i)%p))%p;
printf("%d\n",ans%p);
}
return ;
}
POJ2154 Color的更多相关文章
- POJ2154 Color【 polya定理+欧拉函数优化】(三个例题)
由于这是第一天去实现polya题,所以由易到难,先来个铺垫题(假设读者是看过课件的,不然可能会对有些“显然”的地方会看不懂): 一:POJ1286 Necklace of Beads :有三种颜色,问 ...
- 【数论】【Polya定理】【枚举约数】【欧拉函数】【Java】poj2154 Color
你随便写一下出来,发现polya原理的式子里面好多gcd是相同的,gcd(n,i)=k可以改写成gcd(n/k,i/k)=1,也就是说指数为k的项的个数为phi(n/k),就很好求了,最后除的那个n直 ...
- POJ2154 Color(Polya定理)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11654 Accepted: 3756 Description Bead ...
- POJ2154 Color 【Polya定理 + 欧拉函数】
题目 Beads of N colors are connected together into a circular necklace of N beads (N<=1000000000). ...
- poj2154 Color ——Polya定理
题目:http://poj.org/problem?id=2154 今天学了个高端的东西,Polya定理... 此题就是模板,然而还是写了好久好久... 具体看这个博客吧:https://blog.c ...
- polya/burnside 学习
参考链接: http://www.cnblogs.com/hankers/archive/2012/08/03/2622231.html http://blog.csdn.net/raalghul/a ...
- 置换群和Burnside引理,Polya定理
定义简化版: 置换,就是一个1~n的排列,是一个1~n排列对1~n的映射 置换群,所有的置换的集合. 经常会遇到求本质不同的构造,如旋转不同构,翻转交换不同构等. 不动点:一个置换中,置换后和置换前没 ...
- HDU2481 Toy
Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission ...
- Sgu294He's Circles
Description 有一个长度为N的环,上面写着'X'和'E',问本质不同的环有多少种.(N不超过200000). Input The input file contains a single i ...
随机推荐
- SQL Server 2005 导出包含(insert into)数据的SQL脚本 (使用存储过程) 分类: 数据库
CREATE PROCEDURE dbo.UspOutputData @tablename sysname AS ) ) ) declare @xtype tinyint declare @name ...
- pprof 查看goroutine
package main import ( "net/http" "runtime/pprof" ) var quit chan struct{} = make ...
- 【数据库】 SQL 通配符
[数据库] SQL 通配符 1. % : 替代一个或多个字符 2. _ : 仅替代一个字符 3. [] : 字符列中的任何单一字符 4. [^charlist] 或者 [!charlist] : 不 ...
- 30分钟 带你浅入requirejs源码
因为最近项目想现实一个单页功能,用的是react ,然后看了一下react route,挖槽 gzip后16k? 然后我简单写了一个纯单页(不支持多页的单页,所有入口都经过rewrite跑到index ...
- Unity3d脚本生命周期
如图: 测试脚本: using UnityEngine; public class Test2 : MonoBehaviour { void Awake() { Debug.Log("Awa ...
- jquery框架一点小心得
下面的小事例 主要实现了 一和按ID查找,并获取元素的 value 或 标签内容和一个去字符串空格的小功能能 假设元素id=“myid”: 获取标签内容$("myid").html ...
- [Linux] umount目录提示device is busy的解决方法
使用sshfs等方式挂载的目录出现问题时,使用umount卸载经常提示device is busy,如果仔细阅读错误提示就可以找到命令lsof和fuser命令. 其实原因就是有进程占用当前目录,导致不 ...
- Visual Studio Code 配置Go 开发环境最简单的方法!!!
由于大家都知道的原因,在国内如果想访问go等各种资源,都会遇到某种不可预知的神奇问题.导致在VS Code中安装 go 各种插件都会失败. 于是乎,网上就出现了各种各样的解决方案:什么手动git cl ...
- nopcommerce商城系统--开发者常遇问题清单
原址:http://www.nopcommerce.com/docs/74/frequently-asked-development-questions.aspx 以下是开发者常见问题的清单.也介绍了 ...
- lintcode-76-最长上升子序列
76-最长上升子序列 给定一个整数序列,找到最长上升子序列(LIS),返回LIS的长度. 说明 最长上升子序列的定义: 最长上升子序列问题是在一个无序的给定序列中找到一个尽可能长的由低到高排列的子序列 ...