求助,各位大佬,TLE了,卢卡斯也是TLE(50分)
查看原帖
求助,各位大佬,TLE了,卢卡斯也是TLE(50分)
486166
Incin楼主2021/9/8 15:45
#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
const int mod = 6662333;

int qmi(int a,int b,int p)
{
    int res = 1;
    while(b)
    {
        if(b&1)res = (LL)res * a %p;
        a = (LL)a * a % p;
        b>>=1;
    }
    return res;
}
LL n;
int ans;
int main()
{
    cin>>n;
	for(int b=0;b<=n;b+=2)
	{
		int res=1;
		for(int i=1,j=n;i<=b;i++,j--)
	    {
	        res = (LL)res * j % mod;
	        res = (LL)res * qmi(i,mod-2,mod) % mod;
	    }
	    ans = (ans + res) % mod;;
	}
    printf("%d\n",ans);
    return 0;
}

卢卡斯定理

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int mod = 6662333;

int qmi(int a,int b)
{
	int res =1;
	while(b)
	{
		if(b&1)res =(LL)res * a % mod;
		a = (LL)a * a % mod;
		b >>= 1; 
	}
	return res;
}

int C(int a,int b)
{
	int res =1;
	for(int i=1,j=a;i<=b;i++,j--)
	{
		res = (LL)res * j % mod;
		res = (LL)res * qmi(i,mod -2) % mod;
	}
	return res;
}

int lucas(LL a,LL b)
{
	if(a<mod && b<mod)return C(a,b);
	else return (LL)C(a%mod,b%mod) *lucas(a/mod,b/mod)%mod;
}

int main()
{
	LL n;
	scanf("%lld",&n);
	int res = 0;
	
	for(LL i=0;i<=n;i+=2)
	{
		res =(res + lucas(n,i))% mod; 
	}
	printf("%d",res);
	return 0;
}
2021/9/8 15:45
加载中...