【LOJ #2106】「JLOI2015」有意义的字符串(矩阵快速幂)

传送门

由于有个奇妙的限制
b 2 ≤ d < ( b + 1 ) 2 b^2\le d<(b+1)^2 b2d<(b+1)2

所以

( b − d ) / 2 ) n (b-\sqrt d)/2)^n (bd )/2)n一定是一个小于 1 1 1的小数

加上这个东西
( ( b + d ) / 2 ) n + ( b − d ) / 2 ) n ((b+\sqrt d)/2)^n+(b-\sqrt d)/2)^n ((b+d )/2)n+(bd )/2)n一定是整数
这个形式可以矩乘优化

最后把小数影响减去即可

#include<bits/stdc++.h>
using namespace std;
const int RLEN=1<<20|1;
inline char gc(){
    static char ibuf[RLEN],*ib,*ob;
    (ob==ib)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
    return (ob==ib)?EOF:*ib++;
}
inline int read(){
    char ch=gc();
    int res=0,f=1;
    while(!isdigit(ch))f^=ch=='-',ch=gc();
    while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
    return f?res:-res;
}
#define ll unsigned long long
inline ll readl(){
	char ch=gc();
	ll res=0;bool f=1;
    while(!isdigit(ch))f^=ch=='-',ch=gc();
    while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
    return f?res:-res;
}

#define re register
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define cs const
#define bg begin
#define poly vector<int>
template<class T>inline void chemx(T &a,T b){a<b?a=b:0;}
template<class T>inline void chemn(T &a,T b){a>b?a=b:0;}
cs ll mod=7528443412579576937;
inline ll add(ll a,ll b){return a+b>=mod?a+b-mod:a+b;}
inline void Add(ll &a,ll b){a+=b;a>=mod?a-=mod:0;}
inline ll mul(ll a,ll b){return (a*b-(ll)((long double)a/mod*b)*mod+mod)%mod;}
ll d,b,n;
struct mat{
	ll a[2][2];
	mat(){memset(a,0,sizeof(a));}
	friend inline mat operator *(cs mat &a,cs mat &b){
		mat c;
		for(int i=0;i<2;i++)
		for(int k=0;k<2;k++)
		for(int j=0;j<2;j++){
			Add(c.a[i][j],mul(a.a[i][k],b.a[k][j]));
		}
		return c;
	}
}tmp;
int f[100];
int main(){
	b=readl(),d=readl(),n=readl();
	if(n==0)return cout<<1,0;
	if(n==1)return cout<<b,0;
	tmp.a[0][0]=b,tmp.a[1][0]=(d-b*b)/4,tmp.a[0][1]=1;
	mat res=tmp;
	n-=2;
	for(ll b=n;b;tmp=tmp*tmp,b>>=1)if(b&1)res=res*tmp;
	ll ans=add(mul(res.a[0][0],b),mul(res.a[1][0],2));
	if((n%2==0)&&(b*b!=d))ans--;
	cout<<ans;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值