SCOJ 4423: Necklace polya
4423: Necklace
题目连接:
http://acm.scu.edu.cn/soj/problem.action?id=4423
Description
baihacker bought a necklace for his wife on their wedding anniversary.
A necklace with N pearls can be treated as a circle with N points where the
distance between any two adjacent points is the same. His wife wants to color
every point, but there are at most 2 kinds of color. How many different ways
to color the necklace. Two ways are said to be the same iff we rotate one
and obtain the other.
Input
The first line is an integer T that stands for the number of test cases.
Then T line follow and each line is a test case consisted of an integer N.
Constraints:
T is in the range of [0, 4000]
N is in the range of [1, 1000000000]
N is in the range of [1, 1000000], for at least 75% cases.
Output
For each case output the answer modulo 1000000007 in a single line.
Sample Input
6
1
2
3
4
5
20
Sample Output
2
3
4
6
8
52488
Hint
题意
有一个长度为n的环,环上每个点的颜色有两种,然后你可以旋转
问你本质不同的串有多少种
题解:
ploya裸题
答案ans[i] = 1/n sigma(d|n)phi(d)*2^(n/d)
代码
#include<bits/stdc++.h>
using namespace std;
const int mod = 1e9+7;
long long quickpow(long long m,long long n,long long k)//返回m^n%k
{
long long b = 1;
while (n > 0)
{
if (n & 1)
b = (b*m)%k;
n = n >> 1 ;
m = (m*m)%k;
}
return b;
}
long long phi(long long n)
{
long long tmp=n;
for(long long i=2;i*i<=n;i++)
if(n%i==0)
{
tmp/=i;tmp*=i-1;
while(n%i==0)n/=i;
}
if(n!=1)tmp/=n,tmp*=n-1;
return tmp;
}
void solve()
{
int n;
scanf("%d",&n);
long long ans = 0;
for(int i=1;i*i<=n;i++)
{
if(n%i==0)
{
ans = ans + phi(i)*quickpow(2,(n/i),mod)%mod*quickpow(n,mod-2,mod)%mod;
ans%= mod;
if(i!=n/i)
{
ans = ans + phi(n/i)*quickpow(2,i,mod)%mod*quickpow(n,mod-2,mod)%mod;
ans%= mod;
}
}
}
cout<<ans<<endl;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)solve();
}
SCOJ 4423: Necklace polya的更多相关文章
- [scu 4423] Necklace
4423: Necklace Description baihacker bought a necklace for his wife on their wedding anniversary. A ...
- LightOJ 1419 – Necklace Polya计数+费马小定理求逆元
题意:给你n个珠子可以染成k种颜色,旋转后相同的视为一种,问共有几种情况 思路:开始按照一般的排列组合做发现情况太多且要太多运算,查了下发现此题是组合中Polya定理模板题- 学的浅只能大致一说公式S ...
- POJ 1286 Necklace of Beads(Polya原理)
Description Beads of red, blue or green colors are connected together into a circular necklace of n ...
- poj 1286 Necklace of Beads (polya(旋转+翻转)+模板)
Description Beads of red, blue or green colors are connected together into a circular necklace of ...
- Necklace of Beads(polya计数)
Necklace of Beads Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7451 Accepted: 3102 ...
- Necklace of Beads (polya定理的引用)
Beads of red, blue or green colors are connected together into a circular necklace of n beads ( n &l ...
- POJ 1286 Necklace of Beads(Polya简单应用)
Necklace of Beads 大意:3种颜色的珠子,n个串在一起,旋转变换跟反转变换假设同样就算是同一种,问会有多少种不同的组合. 思路:正规学Polya的第一道题,在楠神的带领下,理解的还算挺 ...
- hdu 1817 Necklace of Beads(Polya定理)
Necklace of Beads Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- poj 2409 Let it Bead && poj 1286 Necklace of Beads(Polya定理)
题目:http://poj.org/problem?id=2409 题意:用k种不同的颜色给长度为n的项链染色 网上大神的题解: 1.旋转置换:一个有n个旋转置换,依次为旋转0,1,2,```n-1. ...
随机推荐
- JS日历控件特效代码layDate
https://www.js-css.cn/a/jscode/date/2015/0405/1461.html
- 2-Python基础语法-内存管理-运算符-程序控制
目录 1 Python 基础语法 1.1 注释 1.2 缩进 1.3 续行 1.4 标识符 1.5 转义序列 1.6 数字 1.7 字符串 1.8 其他 2 Python 运算符 2.1 赋值运算符 ...
- shell中$*与$@的区别
$*所有的位置参数,被作为一个单词 注意:"$*"必须被""引用 $@ 与$*同义,但是每个参数都是一个独立的""引用字串,这就意味着参数被 ...
- springMVC中ajax的实现
function addDebtResult(){ var repayIds=$("#repayIds").val(); var lateFeeDay=$("#repay ...
- 机顶盒 gettimeofday()获取毫秒溢出
最近在写代码的时候遇见了一个bug,在获取当前时间戳的毫秒时,我自己测试的时候总是OK的,但是测试那边总是测不对,之前一直以为是因为我存储的类型的不对,从long long类型从lld改成llu,然后 ...
- spring源码分析---IOC(1)
我们都知道spring有2个最重要的概念,IOC(控制反转)和AOP(依赖注入).今天我就分享一下spring源码的IOC. IOC的定义:直观的来说,就是由spring来负责控制对象的生命周期和对象 ...
- hive(七)hive-运行方式、GUI接口、权限管理
1.Hive运行方式: 命令行方式cli:控制台模式 脚本运行方式(实际生产环境中用最多) JDBC方式:hiveserver2 web GUI接口 (hwi.hue等) 1.1Hive在CLI模 ...
- 【hdoj_1753】大明A+B(大数)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1753 本题要求是,进行多位的小数加法,由于位数很多,所以不能用double类型存储,可以用字符串存储,然后 ...
- MVC – 5.MVC设计模式和.NetMVC框架
MVC模式-设计模式 •控制器(Controller)- 负责转发请求,对请求进行处理. •视图 (View) - 界面设计人员进行图形界面设计. •模型 (Model)-业务逻辑.数据.验证规则.数 ...
- 用 Python实现一个ftp+CRT(不用ftplib)
转载请注明出处http://www.cnblogs.com/Wxtrkbc/p/5590004.html 本来最初的想法是实现一个ftp服务器,用来实现用户的登陆注册和文件的断点上传下载等,结果做着 ...