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. Demo学习: FileUpload

    FileUpload 文件上传,学习TUniFileUpload控件的使用 TUniFileUpload主要属性: Filter: 文件类型过滤,这个属性在web模式下是无效的,UniGUI目前版本还 ...

  2. ASIHTTPRequest的使用(转)

    转载自:http://fushengfei.iteye.com/blog/1147112 博客分类: IOS   原文地址:http://wiki.magiche.net/pages/viewpage ...

  3. Weblogic12c安装与配置详解

    Weblogic是什么Weblogic的安装Weblogic创建域Weblogic管理域Weblogic的应用Weblogic是什么 Weblogic这是我入职以后第一次接触到的词汇,我很陌生,就从我 ...

  4. css3 简单界面动画

    asdasdasdasda asdasdasdasda

  5. struts2拦截器-简单实现非法登录验证

    概念:什么是拦截器 拦截器实现了面向切面的组件,它会影响多个业务对象的公共行为封装到一个个可重用的模块,减少了系统的重复代码,实现高度内聚,确保业务对象的整洁!   为什么使用拦截器 拦截器消除了动作 ...

  6. bnu 4359 无爱编号(规律)

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=4359 [题意]:输入N,表示几位数,0-这个N位数,有多少个满足条件的号码,不满足的情况为出现4,1 ...

  7. mysql 连接多行 合并多行

    group_concat() select group_concat(id) from xxxx -------------------------------------------- id1,id ...

  8. 1483:[HNOI]2009 梦幻布丁 - BZOJ

    Description N个布丁摆成一行,进行M次操作.每次将某个颜色的布丁全部变成另一种颜色的,然后再询问当前一共有多少段颜色.例如颜色分别为1,2,2,1的四个布丁一共有3段颜色. Input 第 ...

  9. css的优先级以及!important的使用

    CSS的优先级应该是作为CSS基本知识而广为知道的,所以CSS入门后,就一直把CSS的优先级记挂在心里(自觉告诉自己这地方很可能会摔跤).起初可能是因为自己的项目经验不够丰富,或者是自己所接触到的项目 ...

  10. 团体程序设计天梯赛-练习集L2-004. 这是二叉搜索树吗

    L2-004. 这是二叉搜索树吗? 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 一棵二叉搜索树可被递归地定义为具有下列性质的 ...