4423: Necklace

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

Author

baihacker

疯狂地模板题,受不了,比赛的时候连这个定理都没听过,还傻乎乎地想了好久,晕死- -
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
#define ll long long
#define N 32000 ll tot;
ll prime[N+];
bool isprime[N+];
ll phi[N+];
void init()
{
memset(phi,-,sizeof(phi));
memset(isprime,,sizeof(isprime));
tot=;
phi[]=;
isprime[]=isprime[]=;
for(ll i=;i<=N;i++)
{
if(isprime[i])
{
prime[tot++]=i;
phi[i]=i-;
}
for(ll j=;j<tot;j++)
{
if(i*prime[j]>N) break;
isprime[i*prime[j]]=;
if(i%prime[j]==)
{
phi[i*prime[j]]=phi[i]*prime[j];
break;
}
else
phi[i*prime[j]]=phi[i]*(prime[j]-);
}
}
}
ll euler(ll n)
{
if(n<=N) return phi[n];
ll ret=n;
for(ll i=;prime[i]*prime[i]<=n;i++)
{
if(n%prime[i]==)
{
ret-=ret/prime[i];
while(n%prime[i]==) n/=prime[i];
}
}
if(n>) ret-=ret/n;
return ret;
}
ll quickpow(ll a,ll b,ll MOD)
{
a%=MOD;
ll ret=;
while(b)
{
if(b&) ret=(ret*a)%MOD;
a=(a*a)%MOD;
b>>=;
}
return ret;
}
ll exgcd(ll a,ll b,ll& x, ll& y)
{
if(b==)
{
x=;
y=;
return a;
}
ll d=exgcd(b,a%b,y,x);
y-=a/b*x;
return d;
}
ll inv(ll a,ll MOD)
{
ll x,y;
exgcd(a,MOD,x,y);
x=(x%MOD+MOD)%MOD;
return x;
}
void solve(ll n,ll MOD)
{
ll i,t1,t2,ans=;
for(i=;i*i<=n;i++)
{
if(n%i==)
{
if(i*i!=n)
{
t1=euler(n/i)%MOD*quickpow(,i,MOD);
t2=euler(i)%MOD*quickpow(,n/i,MOD);
ans=(ans+t1+t2)%MOD;
}
else
ans=(ans+euler(i)*quickpow(,i,MOD))%MOD;
}
}
ans=ans*inv(n,MOD)%MOD;
printf("%d\n",ans);
}
int main()
{
init();
ll T,n;
ll MOD=;
scanf("%lld",&T);
while(T--)
{
scanf("%lld",&n);
solve(n,MOD);
}
return ;
}
 

[scu 4423] Necklace的更多相关文章

  1. SCOJ 4423: Necklace polya

    4423: Necklace 题目连接: http://acm.scu.edu.cn/soj/problem.action?id=4423 Description baihacker bought a ...

  2. SCU - 4441 Necklace(树状数组求最长上升子数列)

    Necklace frog has \(n\) gems arranged in a cycle, whose beautifulness are \(a_1, a_2, \dots, a_n\). ...

  3. SCU 4441 Necklace

    最长上升子序列,枚举. 因为$10000$最多只有$10$个,所以可以枚举采用哪一个$10000$,因为是一个环,所以每次枚举到一个$10000$,可以把这个移到最后,然后算从前往后的$LIS$和从后 ...

  4. HDU5730 Shell Necklace(DP + CDQ分治 + FFT)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5730 Description Perhaps the sea‘s definition of ...

  5. 2016 Multi-University Training Contest 1 H.Shell Necklace

    Shell Necklace Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  6. ACM:SCU 4437 Carries - 水题

    SCU 4437  Carries Time Limit:0MS     Memory Limit:0KB     64bit IO Format:%lld & %llu  Practice  ...

  7. ACM: SCU 4438 Censor - KMP

     SCU 4438 Censor Time Limit:0MS     Memory Limit:0KB     64bit IO Format:%lld & %llu  Practice D ...

  8. ACM: SCU 4440 Rectangle - 暴力

     SCU 4440 Rectangle Time Limit:0MS     Memory Limit:0KB     64bit IO Format:%lld & %llu  Practic ...

  9. BZOJ 4423: [AMPPZ2013]Bytehattan

    Sol 对偶图+并查集. 思路非常好,将网格图转化成对偶图,在原图中删掉一条边,相当于在对偶图中连上一条边(其实就是网格的格点相互连边),每次加边用并查集维护就可以了. 哦对,还要注意边界就是网格外面 ...

随机推荐

  1. json 语义分析

    json 中:元素与值用冒号 ":" 隔开元素与元素用逗号 "," 隔开{} 之间是一个对象, 对象可以层层嵌套[] 表示数组, 数组元素用逗号 ", ...

  2. python del函数

    del用于list列表及字典操作,删除一个或者连续几个元素也可以删除字典指定的key   示例程序如下: >>> a = [-1, 3, 'aa', 85] # 定义一个list&g ...

  3. Windows Service 访问远程共享权限设置

    最近为实现共享目录之间的文件同步,开发了一个Windows Service. 考虑到在拷贝过程中,如果网络忽然抽风访问不了,导致文件拷贝不完整的情况,果断抛弃.Net 自带的 COPY 方法,而使用D ...

  4. Matlab生成.dll文件在.NET中加载失败与平台的关系及解决方案

    问题链接地址:http://bbs.elecfans.com/forum.php?mod=viewthread&tid=207995

  5. Sybase ASE安装过程报错,无法创建数据库设备[AM fork() failed]

    今天同事要搭建一套测试环境,安装开发版的SYBASE ASE 15.03 Windows平台下的,发现安装过程中到了创建数据库设备的环节就开始报错了,报错信息如下: 03/24/14 09:31:44 ...

  6. 制作下拉菜单(PopupList)

    怎样判断是否应当使用下拉菜单 下拉菜单,就是将一系列的选项隐藏,通过单击某一个控件将会弹出一个包含这些选项的列表,在其中选择想要的选项.这样做不但可以节省屏幕空间,也可以让用户在进行选择时更加方便快捷 ...

  7. Delaunay三角剖分

    Bowyer-Watson算法:1.假设已经生成了连接若干个顶点的Delaunay三角网格:2.加入一个新的节点,找出所有外接圆包含新加入节点的三角形,并将这些三角形删除形成一个空洞:3.空洞的节点与 ...

  8. mysql可以运行在不同sql mode模式下面,sql mode模式定义了mysql应该支持的sql语法,数据校验等

    查看默认的sql mode模式:select @@sql_mode;我的数据库是:STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUT ...

  9. Head First 设计模式笔记:单例模式

    单例模式 确保一个类只有一个实例,并提供一个全局访问点. 类图: Singleton static uniqueInstance //其他属性... static getInstance() //其他 ...

  10. 云风的BLOG❳可靠 UDP 传输

    http://mp.weixin.qq.com/s?__biz=MzA3NjYxOTA0MQ==&mid=405432715&idx=1&sn=2e40ceafd4b298e1 ...