标签:des style http color java os io strong for
3 100 4 10 0 0
8 2
#include <iostream> #include <cstdio> #include <cstring> #include <vector> using namespace std; const int maxn = 100000+10; typedef long long ll; ll ans; ll cnt[maxn]; vector<int> prime; int n,m; void exgcd(ll a,ll b,ll& d,ll& x,ll& y) { if(!b) { d = a; x = 1; y = 0; }else{ exgcd(b,a%b,d,y,x); y -= x*(a/b); } } ll inv(ll a,ll n){ ll d,x,y; exgcd(a,n,d,x,y); return d== 1?(x+n)%n:-1; } void init(){ prime.clear(); memset(cnt,0,sizeof cnt); } void getPrime(){ ll tmp = m; for(int i = 2; i*i <= tmp; i++){ if(tmp%i==0){ prime.push_back(i); while(tmp%i==0){ tmp /= i; } } } if(tmp>1){ prime.push_back(tmp); } } void solve(){ getPrime(); ans = 1; ll ret = 1; for(int i = 2; i <= n; i++){ ll fz = 4*i-2,fm = i+1; for(int k = 0; k < prime.size(); k++){ if(fz%prime[k]==0){ while(fz%prime[k]==0){ fz /= prime[k]; cnt[k]++; } } } ret = (ret*fz)%m; for(int k = 0; k < prime.size(); k++){ if(fm%prime[k]==0){ while(fm%prime[k]==0){ fm /= prime[k]; cnt[k]--; } } } if(fm > 1){ ret = (ret*inv(fm,m))%m; } ll tmp = ret; for(int k = 0; k < prime.size(); k++){ for(int kk = 1; kk <= cnt[k]; kk++){ tmp = (tmp*prime[k])%m; } } ans = (ans+tmp)%m; } printf("%I64d\n",ans); } int main(){ while(~scanf("%d%d",&n,&m) && n+m){ init(); solve(); } return 0; }
HDU3240-Counting Binary Trees(Catalan数+求逆元(非互质))
标签:des style http color java os io strong for
原文地址:http://blog.csdn.net/mowayao/article/details/38821217