[scu 4423] Necklace
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的更多相关文章
- SCOJ 4423: Necklace polya
4423: Necklace 题目连接: http://acm.scu.edu.cn/soj/problem.action?id=4423 Description baihacker bought a ...
- SCU - 4441 Necklace(树状数组求最长上升子数列)
Necklace frog has \(n\) gems arranged in a cycle, whose beautifulness are \(a_1, a_2, \dots, a_n\). ...
- SCU 4441 Necklace
最长上升子序列,枚举. 因为$10000$最多只有$10$个,所以可以枚举采用哪一个$10000$,因为是一个环,所以每次枚举到一个$10000$,可以把这个移到最后,然后算从前往后的$LIS$和从后 ...
- HDU5730 Shell Necklace(DP + CDQ分治 + FFT)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5730 Description Perhaps the sea‘s definition of ...
- 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 ...
- ACM:SCU 4437 Carries - 水题
SCU 4437 Carries Time Limit:0MS Memory Limit:0KB 64bit IO Format:%lld & %llu Practice ...
- ACM: SCU 4438 Censor - KMP
SCU 4438 Censor Time Limit:0MS Memory Limit:0KB 64bit IO Format:%lld & %llu Practice D ...
- ACM: SCU 4440 Rectangle - 暴力
SCU 4440 Rectangle Time Limit:0MS Memory Limit:0KB 64bit IO Format:%lld & %llu Practic ...
- BZOJ 4423: [AMPPZ2013]Bytehattan
Sol 对偶图+并查集. 思路非常好,将网格图转化成对偶图,在原图中删掉一条边,相当于在对偶图中连上一条边(其实就是网格的格点相互连边),每次加边用并查集维护就可以了. 哦对,还要注意边界就是网格外面 ...
随机推荐
- unity3d应用内分享(微信、微博等)的实现
问题:如何在unity3d的游戏中实现分享功能,如图 思路: 1.分享功能的实现方式有多种,较方便快捷的一种是直接调用android的API来调出系统的分享界面 2.unity3d里面调用androi ...
- ContextMenuStrip控件
设置单击窗体右键菜单 注意如果想在form1中显示右键菜单,那么要设置form1的属性
- iOS 进阶 第一天(0323)
0323 Storyboard连线错误 如下图: 不允许直接修改对象的结构体属性成员,但允许直接整体修改对象的结构体属性 如下图: 打印一个控件对象的frame 如下图: 如果一个控件无论怎么改变它的 ...
- 【BZOJ 2038】[2009国家集训队]小Z的袜子(hose)
Description HH有一串由各种漂亮的贝壳组成的项链.HH相信不同的贝壳会带来好运,所以每次散步 完后,他都会随意取出一段贝壳,思考它们所表达的含义.HH不断地收集新的贝壳,因此, 他的项链变 ...
- 在Hadoop分布式文件系统的索引和搜索
FROM:http://www.drdobbs.com/parallel/indexing-and-searching-on-a-hadoop-distr/226300241?pgno=3 在今天的信 ...
- JavaBean 和 Map 之间互相转换
JavaBean 和 Map 之间互相转换 import java.beans.BeanInfo; import java.beans.IntrospectionException; import j ...
- Nginx 的启动、停止、平滑重启、信号控制和平滑升级
Nginx 的启动 假设 nginx 安装在 /usr/local/nginx 目录中,那么启动 nginx 的命令就是: [root@localhost ~]# /usr/local ...
- uva 567
Floyd 算法 就输入麻烦点 #include <iostream> #include <cstring> #include <cstdlib> #inclu ...
- Google 面经 09/26
http://www.mitbbs.com/article_t/JobHunting/32539885.html 狗家面经发信站: BBS 未名空间站 (Thu Sep 26 01:20:54 201 ...
- android listview判断是否滑动到顶部还是底部
listView.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStat ...