codeforces 551 D. GukiZ and Binary Operations

D. GukiZ and Binary Operations
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

We all know that GukiZ often plays with arrays.

Now he is thinking about this problem: how many arrays a, of length n, with non-negative elements strictly less then 2l meet the following condition: ? Here operation  means bitwise AND (in Pascalit is equivalent to and, in C/C++/Java/Python it is equivalent to &), operation  means bitwise OR (in Pascal it is equivalent to , in C/C++/Java/Python it is equivalent to |).

Because the answer can be quite large, calculate it modulo m. This time GukiZ hasn't come up with solution, and needs you to help him!

Input

First and the only line of input contains four integers nklm (2 ≤ n ≤ 10180 ≤ k ≤ 10180 ≤ l ≤ 641 ≤ m ≤ 109 + 7).

Output

In the single line print the number of arrays satisfying the condition above modulo m.

Sample test(s)
input
2 1 2 10
output
3
input
2 1 1 3
output
1
input
3 3 2 10
output
9
Note

In the first sample, satisfying arrays are {1, 1}, {3, 1}, {1, 3}.

In the second sample, only satisfying array is {1, 1}.

In the third sample, satisfying arrays are {0, 3, 3}, {1, 3, 2}, {1, 3, 3}, {2, 3, 1}, {2, 3, 3}, {3, 3, 0}, {3, 3, 1}, {3, 3, 2}, {3, 3, 3}.



这道题只要想通在长度为n的0/1串中,相邻两个数不为1的情况的个数为斐波那契数就可以做了。

/*======================================================
# Author: whai
# Last modified:	2015-10-21 09:19
# Filename:		d.cpp
======================================================*/
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <set>
#include <map>

using namespace std;

#define LL __int64
#define PB push_back
#define P pair<int, int>
#define X first
#define Y second

LL a_b_MOD_c(LL a, LL b, LL mod) {
	LL ret = 1;
	a %= mod;
	while(b) {
		if(b & 1) ret = ret * a % mod;
		a = a * a % mod;
		b >>= 1;
	}
	return ret;
}

LL MOD;

struct Matrix {
	LL a[5][5];
	int n, m;
	Matrix(int _n = 0, int _m = 0, LL val = 0) {
		n = _n;
		m = _m;
		for(int i = 0; i < n; ++i) {
			for(int j = 0; j < m; ++j) {
				a[i][j] = (i == j ? val : 0);
			}
		}
	}
	Matrix operator * (Matrix tmp) {
		Matrix ret(n, tmp.m);
		for(int i = 0; i < n; ++i)
			for(int j = 0; j < tmp.m; ++j)
				for(int k = 0; k < m; ++k)
					ret.a[i][j] = (ret.a[i][j] + a[i][k] * tmp.a[k][j]) % MOD;
		return ret;
	}

	Matrix operator ^ (LL b) {
		Matrix ret(n, m, 1), base = (*this);
		while(b) {
			if(b & 1) ret = ret * base;
			base = base * base;
			b >>= 1;
		}
		return ret;
	}
};

bool ok(LL k, LL l) {
	if(l >= 62) return true;
	return k < (1LL << l);
}

LL get_fib(LL n, LL m) {
	MOD = m;
	if(n == 2) return 3;
	Matrix a0(1, 2);
	a0.a[0][0] = 2;
	a0.a[0][1] = 3;
	Matrix m0(2, 2);
	m0.a[0][0] = 0;
	m0.a[0][1] = m0.a[1][0] = m0.a[1][1] = 1;
	m0 = m0 ^ (n - 2);
	a0 = a0 * m0;
	return a0.a[0][1];
}

int main() {
	LL n, k, l, m;
	cin>>n>>k>>l>>m;
	if(!ok(k, l)) {
		puts("0");
		return 0;
	}
	
	LL fib = get_fib(n, m);
	LL ans = 1;
	for(int i = 0; i < l; ++i) {
		if(i >= 62 || (k & (1LL << i)) == 0) {
			ans = (ans * fib) % m;
		} else {
			ans = (ans * (a_b_MOD_c(2, n, m) - fib) % m) % m;
		}
	}
	ans = (ans + m) % m;
	cout<<ans<<endl;
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值