結果

問題 No.2280 FizzBuzz Difference
ユーザー 👑 kmjpkmjp
提出日時 2023-04-21 23:25:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 31 ms / 2,000 ms
コード長 1,872 bytes
コンパイル時間 1,871 ms
コンパイル使用メモリ 196,796 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-24 09:23:50
合計ジャッジ時間 2,514 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 23 ms
5,376 KB
testcase_02 AC 28 ms
5,376 KB
testcase_03 AC 20 ms
5,376 KB
testcase_04 AC 31 ms
5,376 KB
testcase_05 AC 25 ms
5,376 KB
testcase_06 AC 25 ms
5,376 KB
testcase_07 AC 26 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

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;}
//-------------------------------------------------------

int T;
ll M,A,B,K;

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
	if(x<lcm) x+=lcm;
	
	__int128_t v=a1+((a2-a1)%lcm+lcm)*x%lcm*(mo1/g);
	return make_pair(((v%lcm)+lcm) % lcm,lcm);
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>T;
	while(T--) {
		cin>>M>>A>>B>>K;
		ll g=__gcd(A,B);
		
		if(K>A||K%g) {
			cout<<0<<endl;
			continue;
		}
		A/=g;
		B/=g;
		K/=g;
		M/=g;
		ll ret=0;
		if(A==K) {
			M=M/A*A;
			ll numa=M/A;
			ll numb=M/B-M/(A*B);
			ret=numa-1-numb;
		}
		else {
			
			// nA+K=mB;
			auto p=crt(K,A,0,B);
			if(M>=p.first) ret+=(M-p.first)/p.second+1;
			p=crt(0,A,K,B);
			if(M>=p.first) ret+=(M-p.first)/p.second+1;
		}
		cout<<ret<<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