結果

問題 No.2066 Simple Math !
ユーザー kotatsugamekotatsugame
提出日時 2022-08-02 19:30:06
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 303 ms / 2,000 ms
コード長 910 bytes
コンパイル時間 509 ms
コンパイル使用メモリ 65,488 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-09 21:11:39
合計ジャッジ時間 9,526 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 35 ms
4,376 KB
testcase_02 AC 44 ms
4,380 KB
testcase_03 AC 32 ms
4,380 KB
testcase_04 AC 295 ms
4,376 KB
testcase_05 AC 295 ms
4,376 KB
testcase_06 AC 295 ms
4,384 KB
testcase_07 AC 291 ms
4,376 KB
testcase_08 AC 290 ms
4,380 KB
testcase_09 AC 303 ms
4,380 KB
testcase_10 AC 299 ms
4,380 KB
testcase_11 AC 300 ms
4,380 KB
testcase_12 AC 303 ms
4,376 KB
testcase_13 AC 296 ms
4,376 KB
testcase_14 AC 284 ms
4,376 KB
testcase_15 AC 283 ms
4,376 KB
testcase_16 AC 291 ms
4,380 KB
testcase_17 AC 294 ms
4,380 KB
testcase_18 AC 293 ms
4,376 KB
testcase_19 AC 113 ms
4,380 KB
testcase_20 AC 109 ms
4,380 KB
testcase_21 AC 110 ms
4,376 KB
testcase_22 AC 111 ms
4,376 KB
testcase_23 AC 111 ms
4,380 KB
testcase_24 AC 153 ms
4,376 KB
testcase_25 AC 156 ms
4,380 KB
testcase_26 AC 154 ms
4,376 KB
testcase_27 AC 152 ms
4,380 KB
testcase_28 AC 156 ms
4,376 KB
testcase_29 AC 89 ms
4,380 KB
testcase_30 AC 41 ms
4,376 KB
testcase_31 AC 45 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<utility>
using namespace std;
long long floor_sum(long long N,long long M,long long A,long long B)
//Sum[floor((A*i+B)/M),{i,0,N-1}]
{
	long long ans=0;
	if(B>=M)
	{
		ans+=N*(B/M);
		B%=M;
	}
	while(true)
	{
		ans+=N*(N-1)/2*(A/M);
		A%=M;
		long long Ym=(A*N+B)/M,Xm=Ym*M-B;
		if(Ym==0)return ans;
		long long TX=(Xm+A-1)/A;
		ans+=(N-TX)*Ym;
		N=Ym;
		B=TX*A-Xm;
		swap(A,M);
	}
}
int gcd(int a,int b)
{
	while(b)
	{
		int t=a%b;
		a=b;
		b=t;
	}
	return a;
}
long long count(long long X,int P,int Q)
//Sum[floor((X-Pi+Q)/Q),{i,0,Q-1}]
{
	long long R=min((long long)Q,X/P+1);
	return floor_sum(R,Q,P,X+Q-P*(R-1));
}
int main()
{
	int T;
	cin>>T;
	for(;T--;)
	{
		int P,Q,K;
		cin>>P>>Q>>K;
		int g=gcd(P,Q);
		P/=g;
		Q/=g;
		long long l=0,r=(long long)P*Q+K;
		while(r-l>1)
		{
			long long mid=(l+r)/2;
			if(count(mid,P,Q)<=K)l=mid;
			else r=mid;
		}
		cout<<r*g<<endl;
	}
}
0