結果

問題 No.2181 LRM Question 2
ユーザー 👑 kmjpkmjp
提出日時 2023-01-07 11:18:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,570 bytes
コンパイル時間 2,494 ms
コンパイル使用メモリ 208,708 KB
実行使用メモリ 7,484 KB
最終ジャッジ日時 2023-08-21 02:41:26
合計ジャッジ時間 13,526 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
6,584 KB
testcase_01 AC 10 ms
6,700 KB
testcase_02 AC 1,132 ms
7,484 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 16 ms
6,572 KB
testcase_07 AC 16 ms
6,584 KB
testcase_08 AC 1,455 ms
7,424 KB
testcase_09 WA -
testcase_10 AC 1,969 ms
7,304 KB
testcase_11 TLE -
testcase_12 TLE -
testcase_13 AC 10 ms
6,576 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 RE -
testcase_17 AC 52 ms
6,468 KB
testcase_18 WA -
testcase_19 AC 20 ms
6,516 KB
testcase_20 WA -
testcase_21 AC 164 ms
6,816 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------

ll L,R,M;
ll mo;


map<ll,int> enumpr(ll n) {
	map<ll,int> V;
	for(ll i=2;i*i<=n;i++) while(n%i==0) V[i]++,n/=i;
	if(n>1) V[n]++;
	return V;
}

ll ext_gcd(ll p,ll q,ll& x, ll& y) { // get px+qy=gcd(p,q)
	if(q==0) return x=1,y=0,p;
	ll g=ext_gcd(q,p%q,y,x);
	y-=p/q*x;
	return g;
}

pair<ll,ll> crt(ll a1,ll mo1,ll a2,ll mo2) { // return (x,y) y=lcm(a1,a2),x%mo1=a1,x%mo2=a2
	ll g,x,y,z;
	g=ext_gcd(mo1,mo2,x,y);
	a1=(a1%mo1+mo1)%mo1;a2=(a2%mo2+mo2)%mo2;
	if(a1%g != a2%g) return pair<ll,ll>(-1,0); // N/A
	__int128_t lcm=mo1*(mo2/g);
	if(lcm<mo1) return pair<ll,ll>(-2,0); // overflow
	
	__int128_t v=a1+((a2-a1)%lcm+lcm)*x%lcm*(mo1/g);
	return make_pair(((v%lcm)+lcm) % lcm,lcm);
}

ll lucas_ex(ll n,ll r,ll p,ll q) { // C(n,r) % p^q
	const int NUM_=400001;
	static ll fact[NUM_+1], pp, pq;

	if(n<0||r<0||r>n) return 0;
	ll P=1;
	int i;
	ll k=n-r;
	FOR(i,q) P*=p;
	ll mo=P;
	ll e0=0,eq1=0;
	if (fact[0]==0 || p!=pp || q!=pq) {
		pp=p; pq=q;
		fact[0]=1;
		for (int i=1;i<=NUM_;++i) fact[i]=fact[i-1]*i%mo;
	}
	for(ll v=n/p;v;v/=p) e0+=v;
	for(ll v=r/p;v;v/=p) e0-=v;
	for(ll v=k/p;v;v/=p) e0-=v;
	for(ll v=n/P;v;v/=p) eq1+=v;
	for(ll v=r/P;v;v/=p) eq1-=v;
	for(ll v=k/P;v;v/=p) eq1-=v;
	
	
	ll a=1,b=1;
	while(n) {
		a=a*fact[n%p]%P;
		b=b*fact[r%p]%P*fact[k%p]%P;
		n/=p;
		r/=p;
		k/=p;
	}
	
	ll s,t;
	ext_gcd(b,P,s,t);
	ll ret=a*(P+s)%mo;
  
	FOR(i,e0) ret=ret*p%mo;
	if(p>2||q<=2) ret=(mo-ret)%mo;
	
	return ret;
}

ll ret[101010];


void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>L>>R>>M;
	auto m=enumpr(M);
	
	R++;
	ll pmo=1;
	FORR2(a,b,m) {
		mo=1;
		FOR(i,b) mo*=a;
		FOR(i,R-L) {
			ll v=lucas_ex(2*(L+i),L+i,a,b);
			auto p=crt(ret[i],pmo,v,mo);
			ret[i]=p.first;
		}
		pmo*=mo;
	}
	
	ll sum=0;
	FOR(i,R-L) sum+=ret[i]+M-2;
	cout<<sum%M<<endl;
	
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0