poj 2154 Color
这是道标准的数论优化的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的更多相关文章
- 组合数学 - 波利亚定理 --- poj : 2154 Color
Color Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7873 Accepted: 2565 Description ...
- poj 2154 Color——带优化的置换
题目:http://poj.org/problem?id=2154 置换的第二道题! 需要优化!式子是ans=∑n^gcd(i,n)/n (i∈1~n),可以枚举gcd=g,则有phi( n/g )个 ...
- poj 2154 Color < 组合数学+数论>
链接:http://poj.org/problem?id=2154 题意:给出两个整数 N 和 P,表示 N 个珠子,N种颜色,要求不同的项链数, 结果 %p ~ 思路: 利用polya定理解~定理内 ...
- [ACM] POJ 2154 Color (Polya计数优化,欧拉函数)
Color Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7630 Accepted: 2507 Description ...
- poj 2154 Color(polya计数 + 欧拉函数优化)
http://poj.org/problem?id=2154 大致题意:由n个珠子,n种颜色,组成一个项链.要求不同的项链数目.旋转后一样的属于同一种.结果模p. n个珠子应该有n种旋转置换.每种置换 ...
- POJ 2154 Color [Polya 数论]
和上题一样,只考虑旋转等价,只不过颜色和珠子$1e9$ 一样的式子 $\sum\limits_{i=1}^n m^{gcd(i,n)}$ 然后按$gcd$分类,枚举$n$的约数 如果这个也化不出来我莫 ...
- POJ 2154 color (polya + 欧拉优化)
Beads of N colors are connected together into a circular necklace of N beads (N<=1000000000). You ...
- POJ 2154 Color ——Burnside引理
[题目分析] 数据范围有些大. 然后遍求欧拉函数,遍求和就好了,注意取模. [代码] #include <cstdio> #include <cstring> #include ...
- poj 2154 Color 欧拉函数优化的ploya计数
枚举位移肯定超时,对于一个位移i.我们须要的是它的循环个数,也就是gcd(i,n),gcd(i,n)个数肯定不会非常多,由于等价于n的约数的个数. 所以我们枚举n的约数.对于一个约数k,也就是循环个数 ...
随机推荐
- 奇妙华为3c手机, 出现安装未成功问题。
1.我会命令adb uninstall 先卸载我们有签名的安装包. 2.用开发模式在手机上执行我们的应用. 3.然后測试就把手机拿走了,就用应用管理器把我们的应用卸载了. 4.继续装我们的开发包,死活 ...
- delphi 滚屏
滚屏 uses MSHTML;var a: IHTMLDocument2; x,y:Integer;begin y:=y+20; //加减进行上下滚动 a :=WebB ...
- Android学习笔记(20)————利用ListView制作带竖线的多彩表格
http://blog.csdn.net/conowen/article/details/7421805 /********************************************** ...
- python基础(5):深入理解 python 中的赋值、引用、拷贝、作用域
http://my.oschina.net/leejun2005/blog/145911 http://www.cnblogs.com/lulipro/p/5060163.html http://ww ...
- C 语言 .h文件的作用
C语言头文件的作用 最近在工作当中遇到了一点小问题,关于C语言头文件的应用问题,主要还是关于全局变量的定义和声明问题.学习C语言已经有好几年了,工作使用也近半年了,但是对于这部分的东西的确还没有深入的 ...
- [转]一步步教你如何在 Visual Studio 2013 上使用 Github
介绍 我承认越是能将事情变简单的工具我越会更多地使用它.尽管我已经知道了足够的命令来使用Github,但我宁愿它被集成到IDE中.在本教程中,我会告诉你使用Visual Studio 2013如何实现 ...
- XML 格式转JSON 格式
#import <Foundation/Foundation.h> #pragma GCC diagnostic push #pragma GCC diagnostic ignored & ...
- <Think Complexity> 用字典实现图
今天在图书馆闲逛的时候偶然看见<Think Complexity>(复杂性思考)这本书,下午看了一会儿觉得很有意思.本书第二章讲的是用Python实现的图,特别写篇博客记录. 首先,图 ...
- 【Android Studio 小技巧】一键查看文件方法结构目录File Structure
看源代码的时候,如果可以查看class中的所有方法,可以提高效率.Android Studio 中可以使用快捷键一键显示所有方法的目录. Mac: command + fn + F12 (在mac中的 ...
- myeclipse自带客户端连接mysql数据库
一:开启mysql服务器 开始——服务 二:打开myeclipse数据库连接视图 找到DB Browser, 三:新建一个连接 如图: 四:填写相关连接参数,并添加jar包驱动 五:然后一路next, ...