思路

polya定理的模板题,但是还要加一些优化

题目的答案就是

\[\frac{\sum_{i=1}^n n^{gcd(i,n)}}{n}
\]

考虑上方的式子怎么求

因为\(gcd(i,n)\)肯定有很多重复,枚举\(gcd(i,n)\),因为\(gcd(i,n)\)是\(n\)的约数,所以枚举约数

\[\begin{align}&\sum_{d|n}^nn^d\sum_{k=1}^n[gcd(n,k)=d]\\=&\sum_{d|n}^nn^d\sum_{k=1}^{\lfloor\frac{n}{d}\rfloor}[gcd(\lfloor\frac{n}{d}\rfloor,k)=1]\\=&\sum_{d|n}^nn^d\phi(\lfloor\frac{n}{d}\rfloor) \end{align}
\]

然后做完了

一个求phi的方式

\(\phi(i)=i\prod_{p是k的质因数}(1-\frac{1}{p})\)

如果有\(p|n\)且\(p^2|n\),则\(\phi(n)=\phi(n/p)\times p\)

如果有\(p|n\)且\(p^2\not|n\),则\(\phi(n)=\phi(n/p)\times (p-1)\)

代码

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#define int long long
using namespace std;
int n,p,t,ans=0;
int pow(int a,int b){
int ans=1;
while(b){
if(b&1)
ans=(ans*a)%p;
a=(a*a)%p;
b>>=1;
}
return ans;
}
int get_phi(int x){
int ans=x;
int top=sqrt(x+0.5);
for(int i=2;i<=top;i++){
if(x%i==0){
ans=ans/i*(i-1);
while(x%i==0)
x/=i;
}
}
if(x!=1)
ans=ans/x*(x-1);
return ans;
}
signed main(){
scanf("%d",&t);
p=1000000007;
while(t--){
scanf("%lld",&n);
ans=0;
int top=sqrt(n+0.5);
for(int i=1;i<=top&&i*i!=n;i++){
if(n%i==0){
ans=(ans+pow(n,i)*get_phi(n/i)%p+pow(n,n/i)*get_phi(i)%p)%p;
}
}
if(top*top==n)
ans=(ans+pow(n,top)*get_phi(top)%p)%p;
printf("%lld\n",ans*pow(n,p-2)%p);
}
}

P4980 【模板】Polya定理的更多相关文章

  1. [洛谷P4980]【模板】Polya定理

    题目大意:给一个$n$个点的环染色,有$n$中颜色,问有多少种涂色方案是的旋转后本质不同 题解:$burnside$引理:$ans=\dfrac1{|G|}\sum\limits_{g\in G}A_ ...

  2. 等价类计数:Burnside引理 & Polya定理

    提示: 本文并非严谨的数学分析,有很多地方是自己瞎口胡的,仅供参考.有错误请不吝指出 :p 1. 群 1.1 群的概念 群 \((S,\circ)\) 是一个元素集合 \(S\) 和一种二元运算 $ ...

  3. [wikioi2926][AHOI2002]黑白瓷砖(Polya定理)

    小可可在课余的时候受美术老师的委派从事一项漆绘瓷砖的任务.首先把n(n+1)/2块正六边形瓷砖拼成三角形的形状,右图给出了n=3时拼成的“瓷砖三角形”.然后把每一块瓷砖漆成纯白色或者纯黑色,而且每块瓷 ...

  4. HDU 3923 Invoker 【裸Polya 定理】

    参考了http://blog.csdn.net/ACM_cxlove?viewmode=contents           by---cxlove 的模板 对于每一种染色,都有一个等价群,例如旋转, ...

  5. Necklace of Beads (polya定理的引用)

    Beads of red, blue or green colors are connected together into a circular necklace of n beads ( n &l ...

  6. poj1286 Necklace of Beads—— Polya定理

    题目:http://poj.org/problem?id=1286 真·Polya定理模板题: 写完以后感觉理解更深刻了呢. 代码如下: #include<iostream> #inclu ...

  7. poj2154 Color ——Polya定理

    题目:http://poj.org/problem?id=2154 今天学了个高端的东西,Polya定理... 此题就是模板,然而还是写了好久好久... 具体看这个博客吧:https://blog.c ...

  8. Necklace of Beads(polya定理)

    http://poj.org/problem?id=1286 题意:求用3种颜色给n个珠子涂色的方案数.polya定理模板题. #include <stdio.h> #include &l ...

  9. poj 1286 polya定理

    Necklace of Beads Description Beads of red, blue or green colors are connected together into a circu ...

随机推荐

  1. html5-样式的三种方式

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  2. python seek()方法报错:“io.UnsupportedOperation: can't do nonzero cur-relative seeks”

    今天使用seek()时报错了, 看下图 然后就百度了一下,找到了解决方法 这篇博客https://www.cnblogs.com/xisheng/p/7636736.html 帮忙解决了问题, 照理说 ...

  3. mysql的sql执行计划详解

    实际项目开发中,由于我们不知道实际查询的时候数据库里发生了什么事情,数据库软件是怎样扫描表.怎样使用索引的,因此,我们能感知到的就只有 sql语句运行的时间,在数据规模不大时,查询是瞬间的,因此,在写 ...

  4. 限制访问次数例子和Ajax的some

    -- 限制访问次数作业解答 -- -- urls.py 里-- from django.conf.urls import url from django.contrib import admin fr ...

  5. Caterpillar sis service information training and software

    Cat et sis caterpillar heavy duty truck diagnostics repair. Training demonstration allows.cat electr ...

  6. ubuntu重启+sublime快捷键

    sudo reboot Sublime常用快捷键: 掌握基本的代码编辑器的快捷键,能让你打码更有效率,刚开始可能不大记得住,多敲几次就能熟悉并使用它 精华键 : Ctrl+Shift+P:打开命令面板 ...

  7. USB开发库STSW-STM32121文件分析(转)

    源: USB开发库STSW-STM32121文件分析

  8. close yield

    close的方法主要是关闭子生成器,需要注意的有4点: 1.如果生成器close后,还继续next,会报错StopIteration   [图片]     2.如果我捕获了异常,将GeneratorE ...

  9. curl命令基本使用小总结

    curl 下载文件 -o:将文件保存为命令行中指定的文件名的文件中 -O:使用URL中默认的文件名保存文件到本地 --limit-rate:限速 -C:对大文件使用断点续传功能 --silent:不显 ...

  10. 框架frame

    使用框架切分网页 效果: 代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"& ...