Different Circle Permutation
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 208 Accepted Submission(s): 101
Problem Description
You may not know this but it's a fact that Xinghai Square is Asia's largest city square. It is located in Dalian and, of course, a landmark of the city. It's an ideal place for outing any time of the year. And now:
There are N children from a nearby primary school flying kites with a teacher. When they have a rest at noon, part of them (maybe none) sit around the circle flower beds. The angle between any two of them relative to the center of the circle is always a multiple of 2πN but always not 2πN.
Now, the teacher raises a question: How many different ways there are to arrange students sitting around the flower beds according to the rule stated above. To simplify the problem, every student is seen as the same. And to make the answer looks not so great, the teacher adds another specification: two ways are considered the same if they coincide after rotating.
There are N children from a nearby primary school flying kites with a teacher. When they have a rest at noon, part of them (maybe none) sit around the circle flower beds. The angle between any two of them relative to the center of the circle is always a multiple of 2πN but always not 2πN.
Now, the teacher raises a question: How many different ways there are to arrange students sitting around the flower beds according to the rule stated above. To simplify the problem, every student is seen as the same. And to make the answer looks not so great, the teacher adds another specification: two ways are considered the same if they coincide after rotating.
Input
There are T tests (T≤50). Each test contains one integer N. 1≤N≤1000000000 (109). Process till the end of input.
Output
For each test, output the answer mod 1000000007 (109+7) in one line.
Sample Input
4
7
10
7
10
Sample Output
3
5
15
5
15
Source
【传送门】
该题题意:对成环的n个点染黑白两色,其中黑色不能相邻,请问在考虑旋转同构的情况下有几种不一样的方案。
首先不考虑旋转。
对于一个不考虑旋转的f(n),f(n)=f(n-1)+f(n-2)。
对于n=1要特殊判断 因为n=1有两种情况,只涂黑和白,只涂黑不符合上述情况可读入1后直接输出2。
因此f(1)=1,f(2)=3。
我们可以用矩阵快速幂快速算出f(n)。
显然的。对于n个点的环
①可由n-1个点的环在某个固定位置插入0来生成。该位置两边的情况为00,01,10。插入后为000,001,100。(另n-1由n-2的同样操作生成则有 0000,0001,1000,为了与情况②对应故列出)
②可由n-2个点的环在某个固定位置插入01或10来生成。该位置两边的情况为00,01,10。插入后为0100,0010,0101,1010。
至此所有情况已经考虑到了。(0000,0001,1000,0100,0010,0101,1010)
接下来考虑同构。
对于没有约束条件的n个点的环,显然由burnside引理可得方案数$g(n)=frac{1}{n}sum_{i=1}^{n} 2^{gcd(i,j)}$
因此可得(有点难理解,你把他理解成每个循环相邻在一起的几个点要符合黑点不相邻)$g(n)=\frac{1}{n} \sum_{i=1}^{n} f(gcd(i,j))$
即$g(n)= \frac{1}{n} \sum_{d|n}^{} \varphi(\frac{n}{d}) f(d)$
以下为代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#define clr(x) memset(x,0,sizeof(x))
#define mod 1000000007
#define LL long long
using namespace std;
int inf[];
int prime[];
int mart1[][];
int mart2[][];
int mart3[][];
void is_prime();
int eular(int x);
void exgcd(int a,int b,int &x,int &y);
void quickmul(int x);
int fu(int x);
int main()
{
int n;
int xi,yi;
int ans;
is_prime();
while(scanf("%d",&n)!=EOF)
{
ans=;
if(n==)
{
printf("2\n");
continue;
}
for(int i=;i*i<=n;i++)
{
if(i*i==n)
ans=(int)(((LL)ans+(LL)eular(n/i)*(LL)fu(i))%mod);
else if(n%i==)
ans=(int)(((LL)ans+((LL)eular(n/i)*(LL)fu(i))%mod+((LL)eular(i)*(LL)fu(n/i))%mod)%mod);
}
exgcd(n,mod,xi,yi);
xi=(xi%mod+mod)%mod;
ans=(int)((((LL)ans*(LL)xi)%mod+mod)%mod);
printf("%d\n",ans);
}
}
void exgcd(int a,int b,int &x,int &y)
{
if(b==)
{
x=;
y=;
return ;
}
exgcd(b,a%b,y,x);
y-=x*(a/b);
return ;
}
void is_prime()
{
clr(inf);
clr(prime);
inf[]=inf[]==;
int tot=;
for(int i=;i<;i++)
{
if(!inf[i]) prime[tot++]=i;
for(int j=;j<tot;j++)
{
if(i*prime[j]>) break;
inf[i*prime[j]]=;
if(i%prime[j]==) break;
}
}
return ;
}
int eular(int x)
{
int ans=x;
for(int j=;prime[j]*prime[j]<=x;j++)
if(x%prime[j]==)
{
ans-=ans/prime[j];
while(x%prime[j]==)
x/=prime[j];
}
if(x>) ans-=ans/x;
return ans;
}
int fu(int x)
{
if(x==) return ;
if(x==) return ;
clr(mart1);
clr(mart2);
clr(mart3);
mart1[][]=mart1[][]=mart1[][]=mart2[][]=mart2[][]=;
quickmul(x-);
return (int)(((LL)mart2[][]*+(LL)mart2[][]*)%mod);
}
void quickmul(int x)
{
int d;
while(x)
{
if(x&)
{
for(int i=;i<;i++)
for(int j=;j<;j++)
{
d=;
for(int k=;k<;k++)
d=(int)(((LL)d+((LL)mart2[i][k]*(LL)mart1[k][j])%mod)%mod);
mart3[i][j]=d;
}
for(int i=;i<;i++)
for(int j=;j<;j++)
mart2[i][j]=mart3[i][j];
}
x>>=;
for(int i=;i<;i++)
for(int j=;j<;j++)
{
d=;
for(int k=;k<;k++)
d=(int)(((LL)d+((LL)mart1[i][k]*(LL)mart1[k][j])%mod)%mod);
mart3[i][j]=d;
}
for(int i=;i<;i++)
for(int j=;j<;j++)
mart1[i][j]=mart3[i][j];
}
return ;
}