洛谷P5110 块速递推 [分块]
思路
显然可以特征根方程搞一波(生成函数太累),得到结果:
\[a_n=\frac 1 {13\sqrt{337}} [(\frac{233+13\sqrt{337}}{2})^n-(\frac{233+13\sqrt{337}}{2})^n]
\]
(其实我也不知道是不是,网上抄的,懒得算了)
放在模意义下,得到
\[a_n= 233230706\times (94153035^n-905847205^n) \pmod {1e9+7}
\]
后面两个可以分块,预处理出\(x^{[1,\sqrt{{mod}}]}\),再处理出\(x^{\sqrt{mod}\times[1,\sqrt{mod}]}\),就可以\(O(1)\)得到\(x^n\)了。
代码
#include<bits/stdc++.h>
clock_t t=clock();
namespace my_std{
using namespace std;
#define pii pair<int,int>
#define fir first
#define sec second
#define MP make_pair
#define rep(i,x,y) for (int i=(x);i<=(y);i++)
#define drep(i,x,y) for (int i=(x);i>=(y);i--)
#define go(x) for (int i=head[x];i;i=edge[i].nxt)
#define BASE 32768
#define mod 1000000007
#define templ template<typename T>
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
templ inline T rnd(T l,T r) {return uniform_int_distribution<T>(l,r)(rng);}
templ inline bool chkmax(T &x,T y){return x<y?x=y,1:0;}
templ inline bool chkmin(T &x,T y){return x>y?x=y,1:0;}
templ inline void read(T& t)
{
t=0;char f=0,ch=getchar();double d=0.1;
while(ch>'9'||ch<'0') f|=(ch=='-'),ch=getchar();
while(ch<='9'&&ch>='0') t=t*10+ch-48,ch=getchar();
if(ch=='.'){ch=getchar();while(ch<='9'&&ch>='0') t+=d*(ch^48),d*=0.1,ch=getchar();}
t=(f?-t:t);
}
template<typename T,typename... Args>inline void read(T& t,Args&... args){read(t); read(args...);}
char sr[1<<21],z[20];int C=-1,Z=0;
inline void Ot(){fwrite(sr,1,C+1,stdout),C=-1;}
inline void print(register int x)
{
if(C>1<<20)Ot();if(x<0)sr[++C]='-',x=-x;
while(z[++Z]=x%10+48,x/=10);
while(sr[++C]=z[Z],--Z);sr[++C]='\n';
}
void file()
{
#ifndef ONLINE_JUDGE
freopen("a.in","r",stdin);
#endif
}
inline void chktime()
{
#ifndef ONLINE_JUDGE
cout<<(clock()-t)/1000.0<<'\n';
#endif
}
#ifdef mod
ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x%mod) if (y&1) ret=ret*x%mod;return ret;}
ll inv(ll x){return ksm(x,mod-2);}
#else
ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x) if (y&1) ret=ret*x;return ret;}
#endif
// inline ll mul(ll a,ll b){ll d=(ll)(a*(double)b/mod+0.5);ll ret=a*b-d*mod;if (ret<0) ret+=mod;return ret;}
}
using namespace my_std;
namespace Mker
{
unsigned long long SA,SB,SC;
void init(){scanf("%llu%llu%llu",&SA,&SB,&SC);}
inline unsigned long long rand()
{
SA^=SA<<32,SA^=SA>>13,SA^=SA<<1;
unsigned long long t=SA;
SA=SB,SB=SC,SC^=t^SA;return SC;
}
}
struct POW
{
ll a;
ll pow1[BASE+2],pow2[BASE+2];
void init(int aa)
{
a=aa;
pow1[0]=1;rep(i,1,BASE) pow1[i]=pow1[i-1]*a%mod;
pow2[0]=1;rep(i,1,BASE) pow2[i]=pow2[i-1]*pow1[BASE]%mod;
}
inline ll query(register int n){return pow1[n&32767]*pow2[n>>15]%mod;}
}a,b;
int main()
{
file();
a.init(94153035),b.init(905847205);
int T;read(T);Mker::init();
ll ans=0;
rep(i,1,T)
{
ll n=Mker::rand()%(mod-1);
ll cur=233230706ll*(a.query(n)-b.query(n)+mod)%mod;
ans^=cur;
}
cout<<ans;
return 0;
}