【题目分析】

数据范围有些大。

然后遍求欧拉函数,遍求和就好了,注意取模。

【代码】

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; #define F(i,j,k) for (int i=j;i<=k;++i)
#define D(i,j,k) for (int i=j;i>=k;--i)
#define maxn 100005
#define inf 0x3f3f3f3f int n,p,x,sum;
int ispr[maxn],pr[maxn],top=0; void init()
{
F(i,2,maxn-1)
if (!ispr[i])
{
pr[++top]=i;
F(j,2,inf)
{
if (j*i>=maxn) break;
ispr[j*i]=1;
}
}
} int qpow(int a,int b)
{
a%=p;
int ret=1;
while (b)
{
if (b&1) (ret*=a)%=p;
(a*=a)%=p;
b>>=1;
}
return ret;
} int phi(int n)
{
int ret=n;
for (int i=1;pr[i]*pr[i]<=n&&i<=top;++i)
if (n%pr[i]==0)
{
ret=ret-ret/pr[i];
while (n%pr[i]==0) n/=pr[i];
}
if (n>1) ret=ret-ret/n;
return ret%p;
} int main()
{
init();
// F(i,1,top) printf("%d ",pr[i]); printf("\n");
scanf("%d",&x);
while (x--)
{
sum=0;
scanf("%d%d",&n,&p);
for (int i=1;i*i<=n;++i)
{
if (n%i==0)
{
sum=(sum+(qpow(n,i-1)*phi(n/i))%p)%p;
if (i*i!=n) sum=(sum+(qpow(n,n/i-1)*phi(i))%p)%p;
}
}
printf("%d\n",sum);
}
}

  

POJ 2154 Color ——Burnside引理的更多相关文章

  1. 组合数学 - 波利亚定理 --- poj : 2154 Color

    Color Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7873   Accepted: 2565 Description ...

  2. poj 2154 Color——带优化的置换

    题目:http://poj.org/problem?id=2154 置换的第二道题! 需要优化!式子是ans=∑n^gcd(i,n)/n (i∈1~n),可以枚举gcd=g,则有phi( n/g )个 ...

  3. poj 2154 Color < 组合数学+数论>

    链接:http://poj.org/problem?id=2154 题意:给出两个整数 N 和 P,表示 N 个珠子,N种颜色,要求不同的项链数, 结果 %p ~ 思路: 利用polya定理解~定理内 ...

  4. [ACM] POJ 2154 Color (Polya计数优化,欧拉函数)

    Color Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7630   Accepted: 2507 Description ...

  5. poj 2154 Color(polya计数 + 欧拉函数优化)

    http://poj.org/problem?id=2154 大致题意:由n个珠子,n种颜色,组成一个项链.要求不同的项链数目.旋转后一样的属于同一种.结果模p. n个珠子应该有n种旋转置换.每种置换 ...

  6. poj 2154 Color

    这是道标准的数论优化的polya题.卡时卡的很紧,需要用int才能过.程序中一定要注意控制不爆int!!!我因为爆intWA了好久=_=…… 题目简洁明了,就是求 sigma n^gcd(i,n):但 ...

  7. POJ 2154 Color [Polya 数论]

    和上题一样,只考虑旋转等价,只不过颜色和珠子$1e9$ 一样的式子 $\sum\limits_{i=1}^n m^{gcd(i,n)}$ 然后按$gcd$分类,枚举$n$的约数 如果这个也化不出来我莫 ...

  8. POJ 2154 color (polya + 欧拉优化)

    Beads of N colors are connected together into a circular necklace of N beads (N<=1000000000). You ...

  9. poj 2154 Color 欧拉函数优化的ploya计数

    枚举位移肯定超时,对于一个位移i.我们须要的是它的循环个数,也就是gcd(i,n),gcd(i,n)个数肯定不会非常多,由于等价于n的约数的个数. 所以我们枚举n的约数.对于一个约数k,也就是循环个数 ...

随机推荐

  1. iOS 画圆图片的几种方法

    方法一: self.cycleImv= [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 50, 50)]; [self.view addS ...

  2. 创建一个文件夹用于写入UTF-8编码的文件

    实现效果: 知识运用: File类的CreateText方法 StreamWriter类的WriteLine方法 实现代码: private void button2_Click(object sen ...

  3. webpack devserver的说明

    一般我们在项目中 如果用webpack的话,基本都会用到webpack-dev-server,配置大致如下: devServer={ contentBase: basePath, historyApi ...

  4. C++11:移动构造函数的测试

    C++11:移动构造函数的测试 代码如下: #include <iostream> #include <stddef.h> #include <Windows.h> ...

  5. Android 使用 adb命令 远程安装apk

    Android 使用 adb命令 远程安装apk ./adb devices 列出所有设备 ./adb connect 192.168.1.89 连接到该设备 ./adb logcat 启动logca ...

  6. Bootstrap历练实例:表单控件状态(焦点)

    输入框焦点 当输入框 input 接收到 :focus 时,输入框的轮廓会被移除,同时应用 box-shadow. <!DOCTYPE html><html><head& ...

  7. Greenplum介绍-table

    GP中的table和其它关系型数据表是一样的,除了数据被分布在不同的segment以外. 建表时需定义以下几个方面:1. 指定列和数据类型2. 约束3. 分布策略4. 数据存储方式5. 大表分区策略 ...

  8. database---many to many relationships(多对多关系型数据库)

    Many to many Relationships A many-to-many relationship occurs when multiple records in a table are a ...

  9. MySQL 查询优化之 Index Merge

    MySQL 查询优化之 Index Merge Index Merge Intersection 访问算法 Index Merge Union 访问算法 Index Merge Sort-Union ...

  10. DocDokuPLM 2.5安装

    安装记录:(大部分是环境安装和配置) 未完待续.