BZOJ5118: Fib数列2

42 篇文章 0 订阅
32 篇文章 0 订阅

这个2比1简单吧..

P 判一下发现模数是个质数

因为有这个公式 Fn=15[(1+52)n(152)n]
用二次剩余弄出在模这个质数下的 5 ,令 A=1+52,B=152
就是求 t(ANBN) N=2n
N 显然与P互质,套个费马小定理直接快速幂就好了

code:

#include<set>
#include<map>
#include<deque>
#include<queue>
#include<stack>
#include<cmath>
#include<ctime>
#include<bitset>
#include<string>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<complex>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;

ll mul(ll a,ll b,ll mod)
{
    ll re=0;
    for(;b;b>>=1,a=(a+a)%mod) if(b&1)
        re=(re+a)%mod;
    return re;
}
ll pw(ll x,ll k,ll mod)
{
    ll re=1ll;
    for(;k;k>>=1,x=mul(x,x,mod)) if(k&1ll)
        re=mul(re,x,mod);
    return re;
}
ll sqr(ll x,ll mod){return mul(x,x,mod);}
const ll mod = 1125899839733759;

struct E
{
    ll a,b,cn;
    friend inline E operator *(const E &x,const E &y)
    {
        return (E){
                (mul(x.a,y.a,mod)+mul(mul(x.b,y.b,mod),x.cn,mod))%mod,
                (mul(x.b,y.a,mod)+mul(x.a,y.b,mod))%mod,
                x.cn};
    }
};
struct Get_Sqrt
{
    ll alpha,alphan;
    ll f(ll x){return pw(x,(mod-1)>>1,mod);}
    ll pw2(E x,ll k)
    {
        E re=(E){1,0,alphan};
        for(;k;k>>=1,x=x*x) if(k&1ll)
            re=re*x;
        return re.a;
    }
    ll solve(ll n)
    {
        if(!n) return 0;
        if(f(n)==mod-1) return -1;
        alpha=0;
        while(!alpha)
        {
            ll cc=mul(rand(),rand(),mod),cn=(sqr(cc,mod)-n+mod)%mod;
            if(f(cn)==mod-1) alpha=cc,alphan=cn;
        }
        E x=(E){alpha,1,alphan};
        return pw2(x,(mod+1)>>1);
    }
}Sqrt;

ll s5,invs5,inv2=(mod+1ll)>>1,A,B;
ll n;

int main()
{
    //freopen("tmp.in","r",stdin);
    //freopen("tmp.out","w",stdout);

    srand(23333333);

    s5=Sqrt.solve(5); invs5=pw(s5,mod-2,mod);
    A=mul((s5+1),inv2,mod);
    B=mod-pw(A,mod-2,mod);

    int tcase; scanf("%d",&tcase);
    while(tcase--)
    {
        scanf("%lld",&n); n=pw(2,n,mod-1);
        ll re=(pw(A,n,mod)-pw(B,n,mod)+mod)%mod;
        re=mul(re,invs5,mod);
        printf("%lld\n",re);
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值