Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations(数学)
2015-06-18 13:22
429 查看
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 Pascal it is equivalent to and,
in C/C++/Java/Python it is equivalent to &),
operation
means
bitwise OR (in Pascal it is equivalent to
,
inC/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 n, k, l, m (2 ≤ n ≤ 1018, 0 ≤ k ≤ 1018, 0 ≤ l ≤ 64, 1 ≤ m ≤ 109 + 7).
Output
In the single line print the number of arrays satisfying the condition above modulo m.
Sample test(s)
input
output
input
output
input
output
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}.
每一位分开考虑,乘法计数原理,用f[i]表示i个数的某一位进行上述操作为0的方案数,很容易发现f是裴波拉契数列。跪在mod为1和k大于等于2^l上,细心啊!!!
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int mod;
// f[i+1] += f[i] (a[i+1] == 0)
// f[i+1] += f[i-1] (a[i+1] == 1)
struct Matrix
{
ll a[2][2];
Matrix operator * (const Matrix & t) const {
Matrix c;
memset(c.a,0,sizeof(c.a));
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
for(int k = 0; k < 2; k++) {
c.a[i][j] += a[i][k] * t.a[k][j];
c.a[i][j] %= mod;
}
}
}
return c;
}
};
Matrix gao(ll n)
{
Matrix res = {1,0,0,1};
Matrix base = {1,1,1,0};
while(n > 0) {
if(n & 1) res =res * base;
n >>= 1;
base = base * base;
}
Matrix f = {1,1,0,0};
return f * res;
}
ll quick(ll n)
{
ll res = 1,base = 2;
//cout << n<<endl;
while(n > 0) {
if(n & 1)res = (res * base) % mod;
n >>= 1;
base = (base * base) % mod;
}
return res;
}
int main()
{
ios_base::sync_with_stdio(false);
ll n,k,l;
while(cin>>n>>k>>l>>mod) {
ll res = 1,t = gao(n).a[0][0];
ll all = quick(n);
for(int i = 0; i < l; i++) {
if(k & 1) {
res = res * (all - t + mod) % mod;
}else {
res = res * t % mod;
}
k >>= 1;
}
if(k)res = 0;
cout<<res%mod<<"\n";
}
return 0;
}
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 Pascal it is equivalent to and,
in C/C++/Java/Python it is equivalent to &),
operation
means
bitwise OR (in Pascal it is equivalent to
,
inC/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 n, k, l, m (2 ≤ n ≤ 1018, 0 ≤ k ≤ 1018, 0 ≤ l ≤ 64, 1 ≤ 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}.
每一位分开考虑,乘法计数原理,用f[i]表示i个数的某一位进行上述操作为0的方案数,很容易发现f是裴波拉契数列。跪在mod为1和k大于等于2^l上,细心啊!!!
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int mod;
// f[i+1] += f[i] (a[i+1] == 0)
// f[i+1] += f[i-1] (a[i+1] == 1)
struct Matrix
{
ll a[2][2];
Matrix operator * (const Matrix & t) const {
Matrix c;
memset(c.a,0,sizeof(c.a));
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
for(int k = 0; k < 2; k++) {
c.a[i][j] += a[i][k] * t.a[k][j];
c.a[i][j] %= mod;
}
}
}
return c;
}
};
Matrix gao(ll n)
{
Matrix res = {1,0,0,1};
Matrix base = {1,1,1,0};
while(n > 0) {
if(n & 1) res =res * base;
n >>= 1;
base = base * base;
}
Matrix f = {1,1,0,0};
return f * res;
}
ll quick(ll n)
{
ll res = 1,base = 2;
//cout << n<<endl;
while(n > 0) {
if(n & 1)res = (res * base) % mod;
n >>= 1;
base = (base * base) % mod;
}
return res;
}
int main()
{
ios_base::sync_with_stdio(false);
ll n,k,l;
while(cin>>n>>k>>l>>mod) {
ll res = 1,t = gao(n).a[0][0];
ll all = quick(n);
for(int i = 0; i < l; i++) {
if(k & 1) {
res = res * (all - t + mod) % mod;
}else {
res = res * t % mod;
}
k >>= 1;
}
if(k)res = 0;
cout<<res%mod<<"\n";
}
return 0;
}