P2012-拯救世界2【EGF】
正題
題目鏈接:https://www.luogu.com.cn/problem/P2012
題目大意
121212種東西排列成長度為nnn的序列,要求前四種出現(xiàn)奇數(shù)次,后四種出現(xiàn)偶數(shù)次,求方案。TTT組數(shù)據(jù),對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,奇數(shù)就是ex?e?x2\frac{e^x-e^{-x}}{2}2ex?e?x?,偶數(shù)就是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)
然后發(fā)現(xiàn)256256256沒有逆元,但是因?yàn)檫@些底數(shù)都含有256256256的因數(shù)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
小的直接處理就好了
然后發(fā)現(xiàn)這樣還是過不了,那就用擴(kuò)展歐拉定理模上一個φ(109)=4×108\varphi(10^9)=4\times 10^8φ(109)=4×108然后根號分治預(yù)處理一下光速冪就可以過了。
時間復(fù)雜度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; }總結(jié)
以上是生活随笔為你收集整理的P2012-拯救世界2【EGF】的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: P6113-[模板]一般图最大匹配【带花
- 下一篇: 华为双卡双待手机如何设置边打电话边上网