結果

問題 No.1358 [Zelkova 2nd Tune *] 語るなら枚数を...
ユーザー 夜
提出日時 2021-01-23 09:04:18
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 762 ms / 2,000 ms
コード長 1,458 bytes
コンパイル時間 932 ms
コンパイル使用メモリ 94,404 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-28 17:59:55
合計ジャッジ時間 5,567 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 11 ms
4,380 KB
testcase_07 AC 8 ms
4,376 KB
testcase_08 AC 8 ms
4,380 KB
testcase_09 AC 7 ms
4,376 KB
testcase_10 AC 7 ms
4,376 KB
testcase_11 AC 664 ms
4,376 KB
testcase_12 AC 584 ms
4,376 KB
testcase_13 AC 762 ms
4,380 KB
testcase_14 AC 510 ms
4,380 KB
testcase_15 AC 589 ms
4,376 KB
testcase_16 AC 657 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cmath>
#include <stdio.h>
#include <queue>
#include <deque>
#include <cstdio>
#include <set>
#include <map>
#include <bitset>
#include <stack>
#include <cctype>
using namespace std;
long long const mod = 1000000007;
long long gcd(long long a, long long b) {
	if (a % b == 0) {
		return (b);
	}
	else {
		return (gcd(b, a % b));
	}
}
long long extGCD(long long a, long long b, long long& x, long long& y) {
	if (b == 0) {
		x = 1;
		y = 0;
		return a;
	}
	long long d = extGCD(b, a % b, y, x);
	y -= a / b * x;
	return d;
}
int main(){
	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {
		long long n, k, h, y;
		long long a[3];
		cin >> a[0] >> a[1] >> a[2] >> y;
		sort(a, a + 3);
		n = a[2], k = a[1], h = a[0];
		long long g = gcd(k, h);
		long long ans = 0, K, H, l, K1, H1, now = y;
		extGCD(k, h, K, H);
		long long mK = K, mH = H;
		for (int j = 0; j <= y / n; j++) {
			if (now % g == 0){
				K *= now / g, H *= now / g;
				l = k * h / g;
				K1 = l / k;
				H1 = l / h;
				if (K < 0) {
					H -= (-K + K1 - 1) / K1 * H1;
					K += (-K + K1 - 1) / K1 * K1;
				}
				else if (H < 0) {
					K -= (-H + H1 - 1) / H1 * K1;
					H += (-H + H1 - 1) / H1 * H1;
				}
				if (K >= 0 && H >= 0) {
					ans += K / K1 + H / H1 + 1;
					if (ans > mod) {
						ans %= mod;
					}
				}
			}
			now -= n;
			K = mK, H = mH;
		}
		cout << ans << endl;
	}
}
0