这是道标准的数论优化的polya题。卡时卡的很紧,需要用int才能过。程序中一定要注意控制不爆int!!!我因为爆intWA了好久=_=……

题目简洁明了,就是求 sigma n^gcd(i,n);但是由于n很大,所以直接暴力枚举必然会T。于是我们按照这种题的通常思路按gcd的值分类

gcd(i, n) = 1 的个数很明显为 phi(n);

gcd(i, n) = 2 -> gcd(i/2, n/2) = 2 所以个数为 phi(n/2);

这样就ok了, 我们就是要求 sigma phi(n/d) * n^d , 其中 d 是 n 的因数。

上代码:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std; int n, p; int mi(int a)
{
int ans = , zan = n%p;
while (a)
{
if (a & )
ans = (ans * zan) % p;
a >>= ; zan = (zan * zan) % p;
}
return ans;
} int ouler(int now)
{
int ans = now;
for (int i = ; i*i <= now; ++i)
if (!(now % i))
{
ans = ans / i * (i-);
while (!(now % i)) now /= i;
}
if (now > ) ans = ans / now * (now-);
return ans;
} int main()
{
int T; scanf("%d", &T);
while (T--)
{
scanf("%d%d", &n, &p);
int ans = ;
for (int i = ; i*i <= n; ++i)
if (!(n % i))
{
ans = (ans + ouler(n/i)%p*mi(i-)) % p; // 两个函数位置一定不能倒过来
if (i * i != n)
ans = (ans + ouler(i)%p*mi(n/i-)) % p; // 不然会超int!!!
}
printf("%d\n", ans % p);
}
return ;
}

poj 2154 Color的更多相关文章

  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 数论]

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

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

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

  8. POJ 2154 Color ——Burnside引理

    [题目分析] 数据范围有些大. 然后遍求欧拉函数,遍求和就好了,注意取模. [代码] #include <cstdio> #include <cstring> #include ...

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

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

随机推荐

  1. Effective C++ Item 29 为”异常安全”而努力是值得的

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie 经验:异常安全函数即使发生异常也不会泄漏资源或同意不论什么数据结构败坏.这种函数区分为三种 ...

  2. iOS与日期相关的操作

    // Do any additional setup after loading the view, typically from a nib. //得到当前的日期 注意week1是星期天 NSDat ...

  3. Centos 6.5使用Bumblebee关闭N卡,冷却你的电脑

    夏天来了,笔记本装的Centos一直非常热.随着天气的变化,这个问题真的要攻克了.差了下原因可能是双显卡笔记本,N卡驱动不完好,导致风扇狂叫. 昨天安装了nvidia 的显卡驱动本以为时间安静了.但是 ...

  4. springMVC1 springmvc的基础知识

    springmvc第一天 springmvc的基础知识 springmvc课程安排: 第一天: 基础知识 springmvc框架(重点) mvc在b/s系统中应用方式 springmvc框架原理(Di ...

  5. online ddl 使用、测试及关键函数栈

    [MySQL 5.6] MySQL 5.6 online ddl 使用.测试及关键函数栈  http://mysqllover.com/?p=547 本文主要分为三个部分,第一部分是看文档时的笔记:第 ...

  6. PHP对大文件的处理思路

    需求: 现有一个1G左右的日志文件,大约有500多万行, 用php返回最后几行的内容. 在php中,对于文件的读取时,最快捷的方式莫过于使用一些诸如file.file_get_contents之类的函 ...

  7. cocos2d-x使用ant批量打包

    当项目需要在多渠道上线时,要打很多的渠道包,少则几十个,多种几百个.它们的区别一般只是渠道id或部分配置信息不同,这些信息均可写在配置文件中. 例如常见的渠道id不同,一般定义在AndroidMani ...

  8. 视频播放-VideoVIew,Vitamio

    播放视频文件其实并不比播放音频文件复杂,主要是使用 VideoView类来实现的.这个类将视频的显示和控制集于一身,使得我们仅仅借助它就可以完成一个简易的视频播放器.VideoView的用法和 Med ...

  9. use selenium in scrapy webdriver

    https://pypi.python.org/pypi/selenium from selenium import webdriver from selenium.webdriver.common. ...

  10. [改善Java代码]集合中的元素必须做到compareTo和equals同步

    实现了Comparable接口的元素就可以排序,