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,也就是循环个数 ...
随机推荐
- mysql 5.6 binlog组提交1
[MySQL 5.6] MySQL 5.6 group commit 性能测试及内部实现流程 尽管Mariadb以及Facebook在long long time ago就fix掉了这个臭名昭著的 ...
- ubuntu下tomcat启动巨慢分析
在一个ubuntu14新环境部署tomcat,通过CI启动一个应用时,启动耗时达到15分钟之久.仔细看tomcat输出的耗时统计日志发现如下日志: Creation of SecureRandom i ...
- php中var_export与var_dump的区别分析
一 var_dump (PHP 3 >= 3.0.5, PHP 4, PHP 5) var_dump -- 打印变量的相关信息 描述 void var_dump ( mixed expressi ...
- 读logback源码系列文章(五)——Appender --转载
原文地址:http://kyfxbl.iteye.com/blog/1173788 明天要带老婆出国旅游几天,所以这段时间暂时都更新不了博客了,临走前再最后发一贴 上一篇我们说到Logger类的inf ...
- Linux shell 脚本攻略之文件查找与文件列表
摘自:<Linux shell 脚本攻略>
- SCI&EI 英文PAPER投稿经验【转】
英文投稿的一点经验[转载] From: http://chl033.woku.com/article/2893317.html 1. 首先一定要注意杂志的发表范围, 超出范围的千万别投,要不就是浪费时 ...
- 还在用GCD?来看看NSOperation吧
在iOS开发中,谈到多线程,大家第一时间想到的一定是GCD.GCD固然是一套强大的多线程解决方案,能够解决绝大多数的多线程问题,但是他易于上手难于精通且到处是坑的特点也注定了想熟练使用它有一定的难度. ...
- Android 高级UI设计笔记07:RecyclerView 的详解
1. 使用RecyclerView 在 Android 应用程序中列表是一个非常重要的控件,适用场合非常多,如新闻列表.应用列表.消息列表等等,但是从Android 一出生到现在并没有非常 ...
- Flask的部署
当前对部署flask的一些学习: 1.全局安装nginx 1.1 nginx的配置文件怎么写? $ sudo rm /etc/nginx/sites-enabled/default $ sudo to ...
- hibernate 创建session
//1. 创建一个 SessionFactory 对象 SessionFactory sessionFactory = null; //1). 创建 Configuration 对象: 对应 hibe ...