# 【BZOJ3328】PYXFIB（单位根反演，矩阵快速幂）

BZOJ

## 题解

\begin{aligned} \sum_{i=0}^n[k|i]{n\choose i}f_i&=\sum_{i=0}^n{n\choose i}f_i\frac{1}{k}\sum_{j=0}^{k-1}\omega_k^{ji}\\ &=\frac{1}{k}\sum_{j=0}^{k-1}\sum_{i=0}^n{n\choose i}f_i(\omega^j)^i \end{aligned}

（也就是后面那个东西是$(A\omega^j+I)^n$

#include<iostream>
#include<cstdio>
using namespace std;
#define ll long long
{
ll x=0;bool t=false;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=true,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return t?-x:x;
}
ll n;int K,MOD,g,w,ans;
int fpow(int a,int b){int s=1;while(b){if(b&1)s=1ll*s*a%MOD;a=1ll*a*a%MOD;b>>=1;}return s;}
struct Matrix
{
int s[2][2];
void clear(){s[0][0]=s[0][1]=s[1][0]=s[1][1]=0;}
void init(){s[0][0]=s[1][1]=1;s[0][1]=s[1][0]=0;}
int*operator[](int x){return s[x];}
}A,I;
Matrix operator*(Matrix a,Matrix b)
{
Matrix c;c.clear();
for(int i=0;i<2;++i)
for(int j=0;j<2;++j)
for(int k=0;k<2;++k)
c[i][j]=(c[i][j]+1ll*a[i][k]*b[k][j])%MOD;
return c;
}
Matrix operator+(Matrix a,Matrix b)
{
for(int i=0;i<2;++i)
for(int j=0;j<2;++j)
a[i][j]=(a[i][j]+b[i][j])%MOD;
return a;
}
Matrix operator*(Matrix a,int b)
{
for(int i=0;i<2;++i)
for(int j=0;j<2;++j)
a[i][j]=1ll*a[i][j]*b%MOD;
return a;
}
Matrix fpow(Matrix a,ll b)
{
Matrix s;s.init();
while(b){if(b&1)s=s*a;a=a*a;b>>=1;}
return s;
}
int fac[100],tot;
int Getg()
{
int x=MOD-1;tot=0;
for(int i=2;i*i<=x;++i)
if(x%i==0)
{
fac[++tot]=i;
while(x%i==0)x/=i;
}
if(x>1)fac[++tot]=x;
for(int i=2;;++i)
{
bool fl=true;
for(int j=1;j<=tot;++j)
if(fpow(i,(MOD-1)/fac[j])==1){fl=false;break;}
if(fl)return i;
}
}
int main()
{
while(T--)
{
}