BZOJ 1485 [HNOI2009]有趣的數列【卡特蘭大數任意模】
阿新 • • 發佈:2018-11-08
模數可以去任意值。
做法原理就是把數分解分解。
當然可以當作板子來記:
#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define ll long long #define rep(i,x,y) for(ll i=(x);i<=(y);i++) #define red(i,x,y) for(ll i=(x);i>=(y);i--) using namespace std; const ll N=2e6+5; ll n,m,ans=1,id[N],vis[N],num[N],pri[N]; inline ll read() { ll x=0;char ch=getchar();bool f=0; while(ch>'9'||ch<'0'){if(ch=='-')f=1;ch=getchar();} while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();} return f?-x:x; } inline ll mul(ll x,ll y) { return x*y%m; } inline ll ksm(ll x,ll y) { ll ret=1ll; while(y) { if(y&1) ret=mul(ret,x); x=mul(x,x);y>>=1; } return ret; } void File() { freopen("interesting.in","r",stdin); freopen("interesting.out","w",stdout); } int main() { // File(); n=read(),m=read(); rep(i,2,2*n) { if(!vis[i]) { pri[++pri[0]]=i;id[i]=pri[0]; } rep(j,1,pri[0]) { if(pri[j]*i>2*n) break; else { id[pri[j]*i]=j; vis[pri[j]*i]=1; if(i%pri[j]==0) break; } } } rep(i,2,n+1) { ll x=i; while(x>1) { num[id[x]]--;x/=pri[id[x]]; } } rep(i,n+1,2*n) { ll x=i; while(x>1) { num[id[x]]++;x/=pri[id[x]]; } } rep(i,1,pri[0]) ans=mul(ans,ksm(pri[i],num[i])); printf("%lld\n",ans); return 0; }