Codeforces 551D GukiZ and Binary Operations

problem

题意

给定n,l,k,m,假设一个长度为n的数组a,满足a[i] < 2n ,并且有(a[1] and a[2])or(a[2] and a[3]) or ... (a[n-1] and a[n]) = k。问这样子的数组的个数有多少,答案模m。


思路

首先这道题我们要通过二进制位来计算。我们可以知道,对于每个位,可以独立计算。然后我们就可以计算每个位是0的种数。设dpi是长度为i的序列为0的种数。可以推出,dp[i] = dp[i-1] + dp[i-2]所以我们用矩阵快速幂去求出斐波那契数列的值,然后用一下乘法原理即可。
写题解的原因是求斐波那契数列可以不用写矩阵快速幂。我们可以设f[n] = x * f[n -k ] + y * f[n - k -1],对于k = n/2时解出xy的值。这样我们就可以递归的去求解f[n],时间复杂度仍为 O(logn)

AC代码

/*************************************************************************
    > File Name: pd.cpp
    > Author: znl1087
    > Mail: loveCJNforever@gmail.com 
    > Created Time: 六  6/20 09:11:46 2015
 ************************************************************************/

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#define LL long long
using namespace std;
LL n,k,l,m;
LL pow2(LL x){
    if( x == 1) return 2%m;
    LL temp = pow2(x/2);
    temp = (temp * temp)%m;
    if( x & 1)temp = (temp * 2)%m;
    return temp;
}
map<LL,LL> F;

LL fib(LL n){
    if(F.count(n))return F[n];
    LL k = n/2;
    if(n % 2 == 0) return F[n] = ( fib(k) * fib(k) % m + fib(k-1) * fib(k-1) % m)%m;
    else return F[n] = (fib(k) * fib(k+1) % m + fib(k-1) * fib(k) % m)%m;
}
int main(){
    cin>>n>>k>>l>>m;
    if( m == 1 || k >= (1ULL << min(l,63ll)) ){
        cout<<0<<endl;
        return 0;
    }
    F[0] = 1,F[1] = 1;
    LL res = 1;
    LL tot = pow2(n),zero = fib(n+1);
    LL temp = (tot - zero + m)%m;
    //cout<<tot<<endl;
    for(int i=0;i<l;i++){
        if(k & 1)res = res * temp % m;
        else res = res * zero %m;
        k>>=1;
    }
    cout<<res<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值