Niuke winter vacation algorithm basic training camp 6 F. Combination number problem

Portal
Insert picture description here
Idea: Cn0+Cn1+Cn2+…=2 ^ n=(1+1)^n
Cn0-Cn1+Cn2+…=(1-1)^n
Add the above two formulas to
get Cn0+Cn2+…=2^ n-1
and
Insert picture description here
Insert picture description here
to get the answer

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int mod = 998244353;
ll qpow(ll a,ll b)
{
    
    
	ll ans = a%mod;
	ll sum = 1;
	while(b)
	{
    
    
		if(b&1)
		sum = sum*ans%mod;
		ans = ans*ans%mod;;
		b /=2;
	}
	return sum%mod;
}
int main()
{
    
    
	ll n;
	scanf("%lld",&n);
	ll m = n/4;
	if(m%2)
	{
    
    
		ll ans = qpow(4,m*2-1) - qpow(2, m*2-1);
		ans = (ans + mod)%mod;
		printf("%lld\n",(ans+mod)%mod);
	}
	else
	{
    
    
		printf("%lld\n",(qpow(2, m*2-1)%mod + qpow(4, m*2-1)%mod)%mod);
	}
}
	

Guess you like

Origin blog.csdn.net/p15008340649/article/details/114151442