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的更多相关文章

  1. [scu 4423] Necklace

    4423: Necklace Description baihacker bought a necklace for his wife on their wedding anniversary. A ...

  2. LightOJ 1419 – Necklace Polya计数+费马小定理求逆元

    题意:给你n个珠子可以染成k种颜色,旋转后相同的视为一种,问共有几种情况 思路:开始按照一般的排列组合做发现情况太多且要太多运算,查了下发现此题是组合中Polya定理模板题- 学的浅只能大致一说公式S ...

  3. POJ 1286 Necklace of Beads(Polya原理)

    Description Beads of red, blue or green colors are connected together into a circular necklace of n ...

  4. poj 1286 Necklace of Beads (polya(旋转+翻转)+模板)

      Description Beads of red, blue or green colors are connected together into a circular necklace of ...

  5. Necklace of Beads(polya计数)

    Necklace of Beads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7451   Accepted: 3102 ...

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

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

  7. POJ 1286 Necklace of Beads(Polya简单应用)

    Necklace of Beads 大意:3种颜色的珠子,n个串在一起,旋转变换跟反转变换假设同样就算是同一种,问会有多少种不同的组合. 思路:正规学Polya的第一道题,在楠神的带领下,理解的还算挺 ...

  8. hdu 1817 Necklace of Beads(Polya定理)

    Necklace of Beads Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  9. 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. ...

随机推荐

  1. 如何把一篇Word文档里的所有换行符去掉?

    编辑-查找,查找框输入 ^13替换框不输入点击全部替换

  2. linux的主题与图标

    我先在使用arch跟xfce, 速度没得说,偶尔用一下openbox 有一天将xfce的声音给搞没了,完全不知道哪里配置错了,只好将用户文件夹下的所有配置删除,然后重启进入一切又ok啦 说一下主题,小 ...

  3. PEB及LDR链

    PEB地址的取得在NT内核系统中fs寄存器指向TEB结构,TEB+0x30处指向PEB结构,PEB+0x0c处指向PEB_LDR_DATA结构,PEB_LDR_DATA+0x1c处存放一些指向动态链接 ...

  4. Python3 反射及常用的方法

    反射就是通过字符串映射或修改程序运行时的状态.属性.方法 有四个常用方法: hasattr(obj,name_str) 判断一个obj对象是否有对应name_str的方法 getattr(obj,na ...

  5. scrapy shell 用法(慢慢更新...)

    scrapy shell 命令 1.scrapy shell url #url指你所需要爬的网址 2.有些网址数据的爬取需要user-agent,scrapy shell中可以直接添加头文件, 第①种 ...

  6. LFM隐语义模型Latent Factor Model

    实际应用 LFM 模型在实际使用中有一个困难,就是很难实现实时推荐.经典的 LFM 模型每次训练都需要扫描所有的用户行为记录,并且需要在用户行为记录上反复迭代来优化参数,所以每次训练都很耗时,实际应用 ...

  7. Python抓取微博评论

    本人是张杰的小迷妹,所以用杰哥的微博为例,之前一直看的是网页版,然后在知乎上看了一个抓取沈梦辰的微博评论的帖子,然后得到了这样的网址 然后就用m.weibo.cn进行网站的爬取,里面的微博和每一条微博 ...

  8. DedeCms当前位置导航去掉最后的分隔符>

    DedeCms的当前位置导航调用标签{dede:field name=’position’ /},在栏目页里调用的当前位置导航,最后会出现分割符号“>”,如:主页 > DedeCms 模板 ...

  9. ConcurrentMap

    ConcurrentMap接口下有两个重要的实现: ConcurrentHashMap ConcurrentSkipListMap(支持并发排序功能,弥补ConcurrentHashMap) Conc ...

  10. email的传输协议与格式(资源链接)

    以下链接为转载. 传输协议: 发:SMTP 收:POP3, IMAP 格式: MIME