首页 技术 正文
技术 2022年11月8日
0 收藏 559 点赞 2,100 浏览 2008 个字

Color

Description

Beads of N colors are connected together into a circular necklace of N beads (N<=1000000000). Your job is to calculate how many different kinds of the necklace can be produced. You should know that the necklace might not use up all the N colors, and the repetitions that are produced by rotation around the center of the circular necklace are all neglected.

You only need to output the answer module a given number P.

Input

The first line of the input is an integer X (X <= 3500) representing the number of test cases. The following X lines each contains two numbers N and P (1 <= N <= 1000000000, 1 <= P <= 30000), representing a test case.

Output

For each test case, output one line containing the answer.

Sample Input

5
1 30000
2 30000
3 30000
4 30000
5 30000

Sample Output

1
3
11
70
629

Source

【分析】  

  经典的置换和burnside引理应用。只有旋转。

  考虑旋转i个单位,那么循环节有gcd(n,i)个

  可以化出求 sigma(n^gcd(i,n))/n

  根据莫比乌斯反演得 sigma(n^d*phi[n/d])/n   (d|n) 【表示我莫比乌斯反演白学了ORZ

  即sigma(n^(d-1)*phi[n/d]) (d|n)

  就算一算就好了。【不用欧拉筛,筛不到n的,先求出n的质因子,那么d的质因子也在里面,然后根据定义分解质因数求phi

  最后要除以n,不能求逆元哦,每次少乘一个n就好了。

  然而我还WA着!!!!【AC了再放代码吧

  好了AC了:

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define Maxn 35000 int n,p;
int pri[Maxn],phi[Maxn],pl;
int d[Maxn],dl;
bool vis[Maxn]; void init()
{
pl=;
memset(vis,,sizeof(vis));
for(int i=;i<=Maxn-;i++)
{
if(vis[i]==)
{
pri[++pl]=i;
phi[i]=i-;
}
for(int j=;j<=pl;j++)
{
if(i*pri[j]>Maxn-) break;
vis[i*pri[j]]=;
if(i%pri[j]!=) phi[i*pri[j]]=phi[i]*(pri[j]-);
else phi[i*pri[j]]=phi[i]*pri[j];
if(i%pri[j]==) break;
}
}
phi[]=;
// for(int i=2;i<=10;i++) printf("%d ",phi[i]);
// printf("\n");
} int qpow(int a,int b,int p)
{
a%=p;
int ans=;
while(b)
{
if(b&) ans=(ans*a)%p;
a=(a*a)%p;
b>>=;
}
return ans;
} void get_d(int n)
{
dl=;
for(int i=;i<=pl;i++) if(n%pri[i]==)
{
while(n%pri[i]==) n/=pri[i];
d[++dl]=pri[i];
}
if(n!=) d[++dl]=n;
} int gphi(int x)
{
int ans=x;
for(int i=;i<=dl;i++) if(x%d[i]==)
{
ans=ans/d[i]*(d[i]-);
}
return ans;
} int main()
{
int T;
scanf("%d",&T);
init();
while(T--)
{
scanf("%d%d",&n,&p);
get_d(n);
int ans=;
for(int i=;i*i<=n;i++) if(n%i==)
{
ans=(ans+(qpow(n,i-,p)*(gphi(n/i)%p)))%p;
if(i*i!=n) ans=(ans+(qpow(n,n/i-,p)*(phi[i]%p)))%p;
}
printf("%d\n",ans);
}
return ;
}

2017-01-13 11:48:09

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902