【POJ 2154】Color
http://poj.org/problem?id=2154
还是先套上Burnside引理:$$\begin{aligned}
ans & =\sum_{i=1}^n n^{(i,n)-1} \
& = \sum_{d=1}^n [d|n]\sum_{i=1}^n [d|i]\left[\left(\frac id,\frac nd\right)=1\right]n^{d-1} \
& = \sum_{d=1}^n [d|n]n{d-1}\sum_{i=1}{\frac nd}\left[\left(i,\frac nd\right)=1\right] \
& = \sum_{d=1}^n [d|n]n^{d-1}\varphi\left(\frac nd\right)
\end{aligned}$$
因为n非常大,为了方便,求\(\varphi\)时进行质因子分解,求\(\varphi(d)\)单次\(O\left(\frac{\sqrt d}{\log d}\right)\),时间复杂度\(O\left(T\frac{n^{\frac 34}}{\log n}\right)\)。
我是这么写的,时间复杂度非常不科学。还有一种更快的\(O(1)\)求\(\varphi\)的做法,就是先欧拉筛\(\sqrt n\)以内的\(\varphi\)并且预处理n的大于\(\sqrt n\)的质因子\(p_{last}\)(如果有的话),求\(\varphi(d)\)时如果\(d>\sqrt n\),那么返回\(\varphi\left(\frac d{p_{last}}\right)*\left(p_{last} - 1\right)\),否则直接返回\(\varphi(d)\)。时间复杂度\(O\left(T\sqrt n\right)\)。
这么简单的题我竟然说了这么多。。。
#include<cmath>
#include<cstdio>
#include<bitset>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 1000000003;
int n, p, sq, num = 0, prime[100003];
bitset <100003> notp;
void Euler_shai() {
for (int i = 2; i <= sq; ++i) {
if (!notp[i]) prime[++num] = i;
for (int j = 1; j <= num && prime[j] * i <= sq; ++j) {
notp[prime[j] * i] = 1;
if (i % prime[j] == 0)
break;
}
}
}
int ipow(int a, int b) {
int ret = 1, w = a % p;
while (b) {
if (b & 1) (ret *= w) %= p;
(w *= w) %= p;
b >>= 1;
}
return ret;
}
int PHI(int nu) {
int ret = 1;
for (int i = 1; i <= num && prime[i] <= nu; ++i)
if (nu % prime[i] == 0) {
(ret *= ((prime[i] - 1) % p)) %= p;
nu /= prime[i];
while (nu % prime[i] == 0) {
(ret *= (prime[i] % p)) %= p;
nu /= prime[i];
}
}
if (nu != 1) (ret *= ((nu - 1) % p)) %= p;
return ret;
}
int work() {
sq = ceil(sqrt(n)); int ret = 0;
for (int i = 1; i < sq; ++i)
if (n % i == 0) {
(ret += ipow(n, i - 1) * PHI(n / i) % p) %= p;
(ret += ipow(n, n / i - 1) * PHI(i) % p) %= p;
}
if (sq * sq == n) (ret += ipow(n, sq - 1) * PHI(sq) % p) %= p;
return ret;
}
int main() {
sq = sqrt(N);
Euler_shai();
int T; scanf("%d", &T);
while (T--) {
scanf("%d%d", &n, &p);
printf("%d\n", work());
}
return 0;
}
【POJ 2154】Color的更多相关文章
- 【POJ 2154】 Color (置换、burnside引理)
Color Description Beads of N colors are connected together into a circular necklace of N beads (N< ...
- 【POJ 2054】 Color a Tree
[题目链接] http://poj.org/problem?id=2054 [算法] 贪心 [代码] #include <algorithm> #include <bitset> ...
- 【POJ 2777】 Count Color(线段树区间更新与查询)
[POJ 2777] Count Color(线段树区间更新与查询) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4094 ...
- bzoj 2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...
- 【链表】BZOJ 2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 382 Solved: 111[Submit][S ...
- BZOJ2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 284 Solved: 82[Submit][St ...
- BZOJ2293: 【POJ Challenge】吉他英雄
2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 80 Solved: 59[Submit][Stat ...
- BZOJ2287: 【POJ Challenge】消失之物
2287: [POJ Challenge]消失之物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 254 Solved: 140[Submit][S ...
- BZOJ2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 126 Solved: 90[Submit][Sta ...
随机推荐
- 省队集训Day1 睡觉困难综合征
传送门:https://www.luogu.org/problem/show?pid=3613 [题解] 按二进制位分开,对于每一位,用“起床困难综合征”的方法贪心做. 写棵LCT,维护正反两种权值, ...
- 【NOI】2004 郁闷的出纳员
[算法]平衡树(treap) [题解] treap知识见数据结构. 解法,具体细节见程序. #include<cstdio> #include<algorithm> #incl ...
- python模块subprocess学习
当我们想要调用系统命令,可以使用os,commands还有subprocess模块整理如下: os模块: 1. os.system 输出命令结果到屏幕.返回命令执行状态. >>> o ...
- html中的meta标签
1.定义 meta元素提供页面的原信息,位于文档头部 2.必须的属性 content属性 该属性提供名称/值对中的值,使用要与http-equiv或name属性一起使用 3.可选的属性 3.1.htt ...
- Coursera在线学习---第十节.大规模机器学习(Large Scale Machine Learning)
一.如何学习大规模数据集? 在训练样本集很大的情况下,我们可以先取一小部分样本学习模型,比如m=1000,然后画出对应的学习曲线.如果根据学习曲线发现模型属于高偏差,则应在现有样本上继续调整模型,具体 ...
- 一个文档让vim飞起来
原文地址:http://www.cnblogs.com/songfy/p/5635757.html 引言 今天我们特地来讲讲这个vim的配置. vim这东西, 很多人装逼的时候经常会提到, 不过大部分 ...
- python实战===图片转换为字符的源码(转)
#cmd执行命令为>>> python xx.py pic.png#-*- coding:utf-8 -*- from PIL import Image import argpars ...
- Keepalived 安装与简单配置
Keepalived 安装与简单配置 http://sivxy.lofter.com/post/1d21ebb9_7e15000
- SqlServer存储过程中使用事务,示例
create proc pro_GetProTrans @GoodsId int, @Number int, @StockPrice money, @SupplierId int, @EmpId in ...
- linux命令面试题
文件管理命令 (1)Linux的文件系统目录配置要遵循FHS规范,规范定义的两级目录规范如下: /home 每个账号在该目录下都有一个文件夹,进行数据的管理 /usr 有 ...