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. 用CRF做命名实体识别(一)

    用CRF做命名实体识别(二) 用CRF做命名实体识别(三) 用BILSTM-CRF做命名实体识别 博客园的markdown格式可能不太方便看,也欢迎大家去我的简书里看 摘要 本文主要讲述了关于人民日报 ...

  2. weblogic nmap扫描脚本

     CVE-2018-2894 / Nmap利用脚本,可批量批量快速扫描getshell.检测漏洞.利用漏洞 地址:https://github.com/Rvn0xsy/nse_vuln/tree/ma ...

  3. X86控制寄存器和系统地址寄存器

    80386控制寄存器和系统地址寄存器如下表所示.它们用于控制工作方式,控制分段管理机制及分页管理机制的实施. 控制寄存器 CRx BIT31 BIT30—BIT12 BIT11—BIT5 BIT4 B ...

  4. 多个id或class属性相同的元素绑定事件

    <td class="tools"><a href="javascript:void(0);" status="0" na ...

  5. 数据结构与算法之KMP 字符串匹配

    举例来说,有一个字符串"DSFFKFJD KFJLKFDLJFJ IWWJKJFJIA",我想知道,里面是否包含另一个字符串"JFJI",有的话就返回在原字符串 ...

  6. ovirt系统磁盘删除后清理功能验证步骤

    测试步骤主要是针对ovirt系统磁盘的‘删除后清理’功能,如下图所示: 测试如下两种方式: 预置条件: 搭建iscsi服务器,且划分一个11G的盘 勾选删除后清理操作步骤:1 .在linux虚拟机 d ...

  7. html的loadrunner脚本

    Action(){ char strs[20]; lr_start_transaction("api_sync_order");   web_add_header("SO ...

  8. python代码这样写会更优雅

    1.链式比较操作 age = 18 if age > 18 and age < 60: print("young man") pythonic if 18 < a ...

  9. ansible command模块将返回值写入变量

    ansible 中command模块支持 register参数将远程命令执行的输出结果存储在变量中,后续可以在when中对该变量进行检索确定下一步任务. --- - name: cat /etc/re ...

  10. ActiveMQ-Network of brokers集群模式

    概述 在ActiveMQ运行过程中,如果发生某个queue只有生产者没有消费者的情况时,消息就会产生积压.Network of brokers模式通过将积压的消息转发给处于同一network的其它br ...