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. Eclipse-Error:笔记-1

    ylbtech-Eclipse-Error:笔记-1 1.返回顶部 1. Whitelabel Error PageThis application has no explicit mapping f ...

  2. Spring MVC简介 2.5 Spring MVC执行的流程

    package org.fkit.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http ...

  3. yii依赖注入

    为了降低代码耦合程度,提高项目的可维护性,Yii采用多许多当下最流行又相对成熟的设计模式,包括了依赖注入(Denpdency Injection, DI)和服务定位器(Service Locator) ...

  4. Python+unittest 接口自动化测试

    1.封装get.post#!/usr/bin/env python3# -*- coding: utf-8 -*- __author__ = 'hualai yu' import requests c ...

  5. javascirpt之 this、apply、call、bind

    this.apply.call.bind 这又是一个面试经典问题~/(ㄒoㄒ)/~~也是 ES5中众多坑中的一个,在 ES6 中可能会极大避免 this 产生的错误,但是为了一些老代码的维护,最好还是 ...

  6. Windows2012R2 时间同步设置

    Windows2012R2里没有了internet时间,或者Internet时间无法同步成功,都可以尝试使用如下方法. 1.打开命令提示符, 输入:gpedit.msc,打开组策略管理器 2.执行上述 ...

  7. C++调用matlab编程

    C++调用Matlab,实在是不得已的方法.原文链接: http://kylen314.blog.com/2012/12/11/matlab_for_cplusplus/  这是个很善良的博客,只出现 ...

  8. 查看linux机器cpu、内存环境信息

    2C2G,4C4G,8C16G,16C32G 这里C指cpu物理核数,G指总内存大小 # 查看物理CPU个数 cat  /proc/cpuinfo| grep "physical id&qu ...

  9. Rx (Reactive Extensions)介绍

    Reactive Extensions (Rx) 原来是由微软提出的一个综合了异步和基于事件驱动编程的库包,使用可观察序列和LINQ-style查询操作. 使用Rx, 开发者可以用Observable ...

  10. spring cloud(一) eureka

    spring cloud 注册中心 eureka 搭建过程 1.搭建eureka-server 服务端 1.1. 新建boot工程 pom引入依赖 <dependency> <gro ...