HDU 5868 Different Circle Permutation

时间:2024-01-18 11:30:14

公式,矩阵快速幂,欧拉函数,乘法逆元。

$an{s_n} = \frac{1}{n}\sum\limits_{d|n} {\left[ {phi(\frac{n}{d})×\left( {fib(d - 1) + fib(d + 1)} \right)} \right]}$。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c=getchar(); x=;
while(!isdigit(c)) c=getchar();
while(isdigit(c)) {x=x*+c-''; c=getchar();}
} LL n,mod=1e9+; LL extend_gcd(LL a,LL b,LL &x,LL &y)
{
if(a==&&b==) return -;
if(b==){x=;y=;return a;}
LL d=extend_gcd(b,a%b,y,x);
y-=a/b*x;
return d;
} LL mod_reverse(LL a,LL n)
{
LL x,y;
LL d=extend_gcd(a,n,x,y);
if(d==) return (x%n+n)%n;
else return -;
} LL phi(LL n)
{
LL res=n,a=n;
for(int i=;i*i<=a;i++)
{
if(a%i==)
{
res=res/i*(i-);
while(a%i==) a/=i;
}
}
if(a>) res=res/a*(a-);
return res;
} struct Matrix
{
LL A[][];
int R, C;
Matrix operator*(Matrix b);
}; Matrix X, Y, Z; Matrix Matrix::operator*(Matrix b)
{
Matrix c;
memset(c.A, , sizeof(c.A));
int i, j, k;
for (i = ; i <= R; i++)
for (j = ; j <= b.C; j++)
for (k = ; k <= C; k++)
c.A[i][j] = (c.A[i][j] + (A[i][k] * b.A[k][j]) % mod) % mod;
c.R = R; c.C = b.C;
return c;
} void init()
{
memset(X.A, , sizeof X.A);
memset(Y.A, , sizeof Y.A);
memset(Z.A, , sizeof Z.A); Y.R = ; Y.C = ;
for (int i = ; i <= ; i++) Y.A[i][i] = ; X.R = ; X.C = ;
X.A[][]=; X.A[][]=;
X.A[][]=; X.A[][]=; Z.R = ; Z.C = ;
Z.A[][]=; Z.A[][]=;
} LL work(int x)
{
x--;
while (x)
{
if (x % == ) Y = Y*X;
x = x >> ;
X = X*X;
}
Z = Z*Y;
return Z.A[][];
} LL fib(int x)
{
if(x==) return ;
init();
return work(x);
} int main()
{
while(~scanf("%lld",&n))
{
if(n==) { printf("2\n"); continue; }
LL ans=; for(LL i=;i*i<=n;i++)
{
if(n%i!=) continue; LL t=phi(n/i)*((fib(i-)+fib(i+))%mod)%mod;
ans=(ans+t)%mod; if(n/i!=i)
{
t=phi(i)*((fib(n/i-)+fib(n/i+))%mod)%mod;
ans=(ans+t)%mod;
}
} LL ni=mod_reverse(n,mod);
ans=ans*ni%mod;
printf("%lld\n",ans);
}
return ;
}