2021 Niu Ke Winter Holiday Algorithm Basic Training Camp 6 F Problem-Combination Number Problem (Fast Power of Complex Numbers)

Topic link
Insert picture description here
problem solution:
Insert picture description here
code:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<ctime>
#define iss ios::sync_with_stdio(false)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int>pii;
const int MAXN=1e5+5;
const int mod=998244353;
const int inf=0x3f3f3f;
const int p=73;
struct Complex
{
    
         //a+bi
    ll a, b;
    Complex(ll _a, ll _b)
    {
    
    
        a = _a; b = _b;
    }
};

Complex mul(Complex x, Complex y)
{
    
    
    ll a = ((x.a*y.a - x.b*y.b)%mod + mod) % mod;
    //ll a = ((x.a*y.a - 5*x.b*y.b)%mod + mod) % mod;  //a+b\sqrt5_i
    ll b = (x.a*y.b + x.b*y.a)%mod;
    return Complex(a, b);
}

Complex cpow(Complex x, ll n)
{
    
    
    Complex ret(1, 0);
    while(n)
    {
    
    
        if(n&1)  ret = mul(ret, x);
        x = mul(x, x);
        n >>= 1;
    }
    return ret;
}
ll mod_pow(ll x,ll n)
{
    
    
    ll res=1;
    while(n>0)
    {
    
    
        if(n&1) res=res*x%mod;
        x=x*x%mod;
        n>>=1;
    }
    return res;
}

int main()
{
    
    
    int n;
    cin>>n;
    Complex a(1, 1);
    Complex b(1,-1);
    ll ans1=mod_pow(2,n);
    Complex ans2= cpow(a, n);
    Complex ans3= cpow(b, n);
    ll ans=(ans1+ans2.a+ans3.a)%mod;
    ans=ans*mod_pow(4,mod-2)%mod;
    cout<<ans<<endl;
}

Guess you like

Origin blog.csdn.net/weixin_45755679/article/details/114040618