#include <stdio.h>
int main()
{
puts("转载请注明出处[vmurder]谢谢");
puts("网址:blog.csdn.net/vmurder/article/details/45174449");
}
公式推导部分:见大爷博客http://blog.csdn.net/popoqqq/article/details/45148309
公式:
矩阵:
数据中存在
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define mod 7528443412579576937ull
#define T 2
using namespace std;
unsigned long long b,d,n;
unsigned long long qmul(unsigned long long x,unsigned long long p)
{
unsigned long long ret=0;
while(p)
{
if(p&1)ret=(ret+x)%mod;
x=(x<<1)%mod,p>>=1;
}
return ret;
}
struct Mrx
{
unsigned long long x[T][T];
}Ini,Trs,Std,E;
Mrx mul(Mrx a,Mrx b)
{
Mrx c=E;
int i,j,k;
for(i=0;i<T;i++)for(j=0;j<T;j++)for(k=0;k<T;k++)
c.x[i][j]=(c.x[i][j]+qmul(a.x[i][k],b.x[k][j]))%mod;
return c;
}
Mrx power(Mrx x,unsigned long long p)
{
Mrx ret=Std;
while(p)
{
if(p&1)ret=mul(ret,x);
x=mul(x,x),p>>=1;
}
return ret;
}
/*
a[i]=b*a[i-1]+(d-b*b)/4*a[i-2]
a[0]=2,a[1]=b
*/
int main()
{
freopen("jxamfe.in","r",stdin);
freopen("jxamfe.out","w",stdout);
Std.x[0][0]=Std.x[1][1]=1;
cin>>b>>d>>n;
Ini.x[0][0]=b,Ini.x[0][1]=2;
Trs.x[0][0]=b,Trs.x[0][1]=1;
Trs.x[1][0]=d-b*b>>2;
Trs=power(Trs,n);
Ini=mul(Ini,Trs);
cout<<(Ini.x[0][1]-(d!=b*b&&~n&1))%mod<<endl;
return 0;
}
【BZOJ4002】【JLOI2015】有意义的字符串 推公式+矩阵乘法
原文:http://blog.csdn.net/vmurder/article/details/45174449