[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 对偶图+并查集. 思路非常好,将网格图转化成对偶图,在原图中删掉一条边,相当于在对偶图中连上一条边(其实就是网格的格点相互连边),每次加边用并查集维护就可以了. 哦对,还要注意边界就是网格外面 ...
随机推荐
- wxPython 基本框架与运行原理 -- App 与 Frame
<wxPython in Action> chapter 1.2 笔记 wxPython 是 wxWidgets 的 Python 实现,“w” for Microsoft Windows ...
- 《WPF程序设计指南》读书笔记——第9章 路由输入事件
1.使用路由事件 路由事件是一种可以针对元素树中的多个侦听器(而不是仅针对引发该事件的对象)调用处理程序的事件.通俗地说,路由事件会在可视树(逻辑树是其子集)上,上下routed,如果哪个节点上订阅了 ...
- Mac 启用http-dav功能(WebDAV服务器)
启用Mac的WebDAV可以实现文件的上传以及Windows.Linux和Mac之间的数据互传. 客户端使用:windows下使用网上邻居 --> 添加一个网上邻居 --> 输入uplo ...
- 【BZOJ 1087】[SCOI2005]互不侵犯King
Description 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子. Input 只有一行,包 ...
- python解决汉诺塔问题
今天刚刚在博客园安家,不知道写点什么,前两天刚刚学习完python 所以就用python写了一下汉诺塔算法,感觉还行拿出来分享一下 首先看一下描述: from :http://baike.baidu. ...
- mybatis(1):入坑篇
依赖 <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artif ...
- hibernateTemplate的load方法
hibernateTemplate的load方法采用延迟加载,所以应当注意. 如果配置不当,采用此方法获取对象,往往会出现异常: javax.servlet.ServletException: org ...
- wamp设置实现本机IP或者局域网访问 (转)
<Directory /> Options FollowSymLinks AllowOverride None Order deny,allow Allow from all #以前是De ...
- [百度]数组A中任意两个相邻元素大小相差1,在其中查找某个数
一.问题来源及描述 今天看了July的微博,发现了七月问题,有这个题,挺有意思的. 数组A中任意两个相邻元素大小相差1,现给定这样的数组A和目标整数t,找出t在数组A中的位置.如数组:[1,2,3,4 ...
- Android Activity交互及App交互
Android交互--------->Intent Activity之间----->Explicit Intent App之间--------->Implicit Intent