正题
题目链接:https://www.luogu.com.cn/problem/P2012
题目大意
121212种东西排列成长度为nnn的序列,要求前四种出现奇数次,后四种出现偶数次,求方案。TTT组数据,对10910^9109取模。
1≤n<263,1≤T≤2×1051\leq n< 2^{63},1\leq T\leq 2\times 10^51≤n<263,1≤T≤2×105
解题思路
显然是EGFEGFEGF,没有限制的话就是exe^xex,奇数就是ex−e−x2\frac{e^x-e^{-x}}{2}2ex−e−x,偶数就是ex+e−x2\frac{e^{x}+e^{-x}}{2}2ex+e−x,这些都是老生常谈了。
然后答案就是
n!×(ex−e−x2)4(ex+e−x2)4(ex)4n!\times (\frac{e^x-e^{-x}}{2})^4(\frac{e^x+e^{-x}}{2})^4(e^{x})^4n!×(2ex−e−x)4(2ex+e−x)4(ex)4
然后解出来就是
F(x)=n!×1256×(e12x−4e8x+6e4x−4+e−4x)F(x)=n!\times \frac{1}{256}\times(e^{12x}-4e^{8x}+6e^{4x}-4+e^{-4x})F(x)=n!×2561×(e12x−4e8x+6e4x−4+e−4x)
⇒F(x)[n]=1256×(12n−4×8n+6×4n−4+(−4)n)\Rightarrow F(x)[n]=\frac{1}{256}\times(12^n-4\times 8^n+6\times 4^{n}-4+(-4)^n)⇒F(x)[n]=2561×(12n−4×8n+6×4n−4+(−4)n)
然后发现256256256没有逆元,但是因为这些底数都含有256256256的因数222所以
=81×12n−4−8n−2+6×4n−4+(−4)n−4=81\times 12^{n-4}-8^{n-2}+6\times 4^{n}-4+(-4)^{n-4}=81×12n−4−8n−2+6×4n−4+(−4)n−4
小的直接处理就好了
然后发现这样还是过不了,那就用扩展欧拉定理模上一个φ(109)=4×108\varphi(10^9)=4\times 10^8φ(109)=4×108然后根号分治预处理一下光速幂就可以过了。
时间复杂度O(20000+T)O(20000+T)O(20000+T)
code
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cctype>
#define ll long long
using namespace std;
const ll b[5]={0,0,0,0,24},T=20000,N=T+10,P=1e9,Phi=4e8;
ll n,pw2[N],pw3[N],Pw2[N],Pw3[N];
ll read(){ll x=0,f=1;char c=getchar();while(!isdigit(c)){if(c=='-')f=-f;c=getchar();}while(isdigit(c))x=(x<<1)+(x<<3)+c-48,c=getchar();return x*f;
}
void print(ll x)
{if(x>9)print(x/10);putchar(x%10+48);return;}
ll G4(ll n)
{n%=Phi;return Pw2[n/T]*Pw2[n/T]%P*pw2[n%T]%P*pw2[n%T]%P;}
ll G8(ll n)
{n%=Phi;return Pw2[n/T]*pw2[n%T]%P*G4(n)%P;}
ll G12(ll n)
{n%=Phi;return Pw3[n/T]*pw3[n%T]%P*G4(n)%P;}
signed main()
{pw2[0]=pw3[0]=Pw2[0]=Pw3[0]=1;for(ll i=1;i<=T;i++)pw2[i]=pw2[i-1]*2ll%P,pw3[i]=pw3[i-1]*3ll%P;for(ll i=1;i<T;i++)Pw2[i]=Pw2[i-1]*pw2[T]%P,Pw3[i]=Pw3[i-1]*pw3[T]%P;while(1){n=read();if(!n)break;if(n<=4){print(b[n]),putchar('\n');continue;}ll ans=81ll*G12(n-4);ans=ans-G8(n-2);ans=ans+6ll*G4(n-4);ans=ans+((n&1)?-1:1)*G4(n-4);print((ans%P+P)%P);putchar('\n'); }return 0;
}