欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

51nod 1436 方程的解数

程序员文章站 2024-03-21 11:31:28
...

https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1436

51nod 1436 方程的解数

大概就是一个按位分析k再加上快速幂就可以了

#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <queue>
#include <cstdio>
#include <map>
#include <set>
#include <utility>
#include <stack>
#include <cstring>
#include <cmath>
#include <vector>
#include <ctime>
#include <bitset>
using namespace std;
#define pb push_back
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d%d",&n,&m)
#define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define sld(n) scanf("%lld",&n)
#define sldd(n,m) scanf("%lld%lld",&n,&m)
#define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k)
#define sf(n) scanf("%lf",&n)
#define sff(n,m) scanf("%lf%lf",&n,&m)
#define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k)
#define ss(str) scanf("%s",str)
#define ansn() printf("%d\n",ans)
#define lansn() printf("%lld\n",ans)
#define r0(i,n) for(int i=0;i<(n);++i)
#define r1(i,e) for(int i=1;i<=e;++i)
#define rn(i,e) for(int i=e;i>=1;--i)
#define mst(abc,bca) memset(abc,bca,sizeof abc)
#define lowbit(a) (a&(-a))
#define all(a) a.begin(),a.end()
#define pii pair<int,int>
#define pll pair<long long,long long>
#define mp(aa,bb) make_pair(aa,bb)
#define lrt rt<<1
#define rrt rt<<1|1
#define X first
#define Y second
#define PI (acos(-1.0))
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll mod = 1000000007 ;
const double eps=1e-9;
const int inf=0x3f3f3f3f;
//const ll infl = 100000000000000000;//1e17
const int maxn=  2e5+20;
const int maxm = 3e2+20;
//muv[i]=(p-(p/i))*muv[p%i]%p;
int in(int &ret) {
    char c;
    int sgn ;
    if(c=getchar(),c==EOF)return -1;
    while(c!='-'&&(c<'0'||c>'9'))c=getchar();
    sgn = (c=='-')?-1:1;
    ret = (c=='-')?0:(c-'0');
    while(c=getchar(),c>='0'&&c<='9')ret = ret*10+(c-'0');
    ret *=sgn;
    return 1;
}

ll mo;
struct mat
{
    ll m[2][2];
    void init()
    {
        mst(m,0);
        m[0][0]= m[0][1] = m[1][0] = 1;
    }
    void clr()
    {
        mst(m,0);
        r0(i,2)m[i][i] = 1;
    }
    void con()
    {
        mst(m,0);
        m[0][0] = 1;
        m[1][0] = 1;
    }
    mat operator *(const mat o)const
    {
        mat t;
        mst(t.m,0);
        r0(i,2)r0(j,2)r0(k,2)
        {
            t.m[i][j] += m[i][k]*o.m[k][j]%mo;
            t.m[i][j] %= mo;
        }
        return t;
    }
};
ll qpow(ll x,ll k)
{
    ll res = 1;
    while(k)
    {
        if(k&1)res = res*x%mo;
        x = x*x%mo;
        k>>=1;
    }
    return res;
}
ll cntbit(ll x)
{
    int cb = -1;
    while(x)
    {
        x>>=1;
        ++cb;
    }
    return cb;
}
ll calfib(ll k)
{
    mat base,first,res;
    res.con();
    base.init();
    first.clr();
    while(k)
    {
        if(k&1)first = first * base;
        base = base *base;
        k>>=1;
    }
    res = first *res;
    return res.m[0][0]%mo;
}
int main() {
#ifdef LOCAL
    freopen("input.txt","r",stdin);
//    freopen("output.txt","w",stdout);
#endif // LOCAL

    ll n,k,l;
    slddd(n,k,l);
    sld(mo);
    if(mo==1||cntbit(k)>=l)
    {
        return puts("0")*0;
    }
    ll x = calfib(n);
    ll y = qpow(2,n) - x;
    y = (y+mo)%mo;
    int p = 0,q = 0;
    while(k)
    {
        if(k&1)++p;
        else ++q;
        k>>=1;
    }
    q += l-p-q;
    ll ans = 1;
    ans = ans * qpow(x,q)%mo;
    ans = ans * qpow(y,p)%mo;
    lansn();
    return 0;
}