按照这个规律找出来的不断取模之下会得负数。需要(ans+mod)%mod,化为正数。
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define ll __int64 const ll mod=530600414; const int maxn=201399; ll w[maxn],ans[maxn],num[maxn],len[maxn]; void init() { int n=201318,i; len[1]=1;len[2]=2; for(i=3;i<=n;i++) len[i]=(len[i-1]+len[i-2])%mod; for(i=0;i<=4;i++) ans[0]=0; num[0]=0;num[1]=0;num[2]=0; w[4]=3;w[3]=1; num[3]=1;num[4]=1; for(i=5;i<=n;i++) { ans[i]=((ans[i-1]+ans[i-2])%mod+ ((len[i-2]*num[i-2]%mod-w[i-2])%mod)*num[i-1]%mod+w[i-1]*num[i-2]%mod)%mod; num[i]=(num[i-1]+num[i-2])%mod; w[i]=(num[i-1]*len[i-2]%mod+(w[i-1]+w[i-2])%mod)%mod; } } int main() { int i,j,n,t; init(); while(scanf("%d",&t)==1) { int cas=1; while(t--) { scanf("%d",&n); printf("Case #%d: %I64d\n",cas++,(ans[n]+mod)%mod); } } return 0; }
时间: 2024-10-13 16:09:38