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

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

POJ Monthly,Lou Tiancheng

解题思路:

转载:http://blog.csdn.net/tsaid/article/details/7366708

题意:给出两个整数n和p,代表n个珠子,n种颜色,要求不同的项链数,翻转置换不考虑。

结果模p.

题解:

我们知道gcd(i,n)表示了循环节的个数。

比如gcd(2,6) = 2, 它的详细过程为:[1。3。5] [2。4,6]

对于随意一个循环置换,他全部循环节的长度为 n / gcd(i,n),在上面的样例中: 循环节长度 = 6 / gcd(2,6) = 3

为了方便说明。用L表示循环节的长度,显然 L | n

假设我们枚举L,求出对于每个L有多少个i, 使得 L = n / gcd (i,n), 那么我们实际上也得到了循环节个数为 n / L 的置换个数。

将L = n / gcd (i,n)转换一下得到:n / L = gcd(i,n )

设 cnt = n / L = gcd(i, n)  注:cnt表示循环节个数,L表示每个循环节的长度

由于 cnt | i, 所以可令 i = cnt * t; ( 由于0 <= i < n, 所以0 <= t < n / cnt = L )

又由于 cnt = n / L, 所以 n = cnt * L;

则 gcd ( i, n ) = gcd ( cnt*t, cnt*L ) = cnt;  ①

可知当 gcd ( t, L ) = 1 时 ① 式成立。

因为 gcd ( t, L ) = 1 的个数就是 Euler(L)的个数。

所以我们能够得到结论:循环节个数为n/L的置换有Euler(L)个。

代码:

#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
bool isprime[50001];
int prime[50001];
int len=0;;
int n,p; void sieve()
{
for(int i=0;i<=50000;i++)
isprime[i]=1;
isprime[0]=isprime[1]=0;
for(int i=2;i<=50000;i++)
{
if(isprime[i])
{
prime[len++]=i;
for(int j=2*i;j<=50000;j+=i)
isprime[j]=0;
}
}
} int euler(int n)
{
int res=n;
for(int i=0;i<len&&prime[i]*prime[i]<=n;i++)
{
if(n%prime[i]==0)
{
res=res/prime[i]*(prime[i]-1);
while(n%prime[i]==0)
n/=prime[i];
}
}
if(n>1)
res=res/n*(n-1);
return res;
} int pow(int p,int n,int mod)
{
int ans=1;
p=p%mod;
while(n)
{
if(n&1)
ans=ans*p%mod;
p=p*p%mod;
n/=2;
}
return ans;
} int main()
{
sieve();
int t;
scanf("%d",&t);
while(t--)
{
int ans=0;
scanf("%d%d",&n,&p);
for(int i=1;i*i<=n;i++)
if(n%i==0)
{
ans=(ans+euler(i)%p*pow(n,n/i-1,p))%p;//注意取余的位置。 euler(i)%p不取余就WA
if(i*i==n)//仅仅要一个i就能够了
break;
ans=(ans+euler(n/i)%p*pow(n,i-1,p))%p;
}
printf("%d\n",ans);
}
return 0;
}

[ACM] POJ 2154 Color (Polya计数优化,欧拉函数)的更多相关文章

  1. POJ 2154 【POLYA】【欧拉】

    前记: TM终于决定以后干啥了.这几天睡的有点多.困饿交加之间喝了好多水.可能是灌脑了. 切记两件事: 1.安心当单身狗 2.顺心码代码 题意: 给你N种颜色的珠子,串一串长度问N的项链,要求旋转之后 ...

  2. POJ-2888 Magic Bracelet(Burnside引理+矩阵优化+欧拉函数+逆元)

    Burnside引理经典好题呀! 题解参考 https://blog.csdn.net/maxwei_wzj/article/details/73024349#commentBox 这位大佬的. 这题 ...

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

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

  4. POJ 2154 Color [Polya 数论]

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

  5. 题解报告:poj 2480 Longge's problem(欧拉函数)

    Description Longge is good at mathematics and he likes to think about hard mathematical problems whi ...

  6. poj3696 快速幂的优化+欧拉函数+gcd的优化+互质

    这题满满的黑科技orz 题意:给出L,要求求出最小的全部由8组成的数(eg: 8,88,888,8888,88888,.......),且这个数是L的倍数 sol:全部由8组成的数可以这样表示:((1 ...

  7. POJ 2773 Happy 2006【GCD/欧拉函数】

    根据欧几里德算法,gcd(a,b)=gcd(a+b*t,b) 如果a和b互质,则a+b*t和b也互质,即与a互质的数对a取模具有周期性. 所以只要求出小于n且与n互质的元素即可. #include&l ...

  8. POJ 3696 The Luckiest number (欧拉函数,好题)

    该题没思路,参考了网上各种题解.... 注意到凡是那种11111..... 22222..... 33333.....之类的序列都可用这个式子来表示:k*(10^x-1)/9进而简化:8 * (10^ ...

  9. POJ 3090 Visible Lattice Points 【欧拉函数】

    <题目链接> 题目大意: 给出范围为(0, 0)到(n, n)的整点,你站在(0,0)处,问能够看见几个点. 解题分析:很明显,因为 N (1 ≤ N ≤ 1000) ,所以无论 N 为多 ...

随机推荐

  1. 杂项-DB:OLAP(联机分析处理)

    ylbtech-杂项-DB:OLAP(联机分析处理) 联机分析处理OLAP是一种软件技术,它使分析人员能够迅速.一致.交互地从各个方面观察信息,以达到深入理解数据的目的.它具有FASMI(Fast A ...

  2. [Pulgin] 前端上传组件Plupload使用指南

    我之前写过一篇文章<文件上传利器SWFUpload使用指南>,里面介绍了上传组件SWFUpload的使用方法,但现在随着html5技术的逐渐推广和普及,再去使用以flash为上传手段的SW ...

  3. [源码管理] ubuntu中svn简明用法:服务器搭建+客户端使用

    本文是对网络上前人的优秀文章加以实践验证后所整理(修正或补充) 第一部分:svn服务器搭建(主要是四步走) 参考:http://www.son1c.cn/show/920.html 一,安装Subve ...

  4. My first blog for java

    我的第一个java程序: package com.hellojava; /** * @author 沽-名-钓-誉 */ public class HelloJava{ /** * @param 输出 ...

  5. C#中图片转换为Base64编码,Base64编码转换为图片

    #region 图片转为base64编码的字符串 public string ImgToBase64String(string Imagefilename) { try { Bitmap bmp = ...

  6. Android网络编程随想录(3)

    大多数Android的app都会使用HTTP协议来发送和接收数据.在Android开发中,通常使用两种http客户端:一个是Apache的HttpClient,另一个是HttpURLConnectio ...

  7. python课程设计笔记(三)turtle绘图库(海龟库)

    实例:绘制一条蟒蛇 #turtle:绘图库(海龟库) import turtle turtle.setup(650,350,200,200) turtle.penup() turtle.fd(-250 ...

  8. 织梦忘记密码DedeCMS密码重设工具radminpass找回密码

    本工具是用于新人忘记管理员密码重设所制作,只需要将radminpass.php文件拷贝到根目录,运行“http://yousite/radminpass.php(yousite为网站域名)”,按照操作 ...

  9. 关于生sql中的空值

    在数据库中的空值无非就是两种形式的表示 一种是什么都没有,一种是以NuLL显示的 , 那么在C# 读取出来怎么判断呢, DtStatus.Rows[0]["FetchCode"]. ...

  10. JDK1.7源码阅读tools包之------ArrayList,LinkedList,HashMap,TreeMap

    1.HashMap 特点:基于哈希表的 Map 接口的实现.此实现提供所有可选的映射操作,并允许使用 null 值和 null 键.(除了非同步和允许使用 null 之外,HashMap 类与 Has ...