結果

問題 No.1899 L1 Cafe
ユーザー ShirotsumeShirotsume
提出日時 2022-01-31 19:33:48
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,545 ms / 2,000 ms
コード長 958 bytes
コンパイル時間 3,503 ms
コンパイル使用メモリ 76,812 KB
実行使用メモリ 59,208 KB
最終ジャッジ日時 2024-05-06 02:03:49
合計ジャッジ時間 21,495 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 133 ms
41,172 KB
testcase_01 AC 1,509 ms
58,696 KB
testcase_02 AC 1,511 ms
58,820 KB
testcase_03 AC 1,096 ms
58,032 KB
testcase_04 AC 1,217 ms
58,320 KB
testcase_05 AC 825 ms
53,956 KB
testcase_06 AC 1,535 ms
58,972 KB
testcase_07 AC 1,446 ms
59,188 KB
testcase_08 AC 1,413 ms
58,424 KB
testcase_09 AC 1,545 ms
58,592 KB
testcase_10 AC 1,533 ms
58,840 KB
testcase_11 AC 1,444 ms
58,536 KB
testcase_12 AC 1,454 ms
59,208 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;
public class Main {
	  public static void main(String[] args) {
	        Scanner sc = new Scanner(System.in);
	        int t = sc.nextInt();
	        for(int i = 0; i < t; i++){
	        	long n = sc.nextLong();
		        long a = sc.nextLong();
		        long b = sc.nextLong();
		        long na = floorSum(n/a, 1, a, n - a * (n/a));
		        long nb = floorSum(n, b, 1, 0);
		        long nab = floorSum(n/a, b, a, n - a * (n/a));
		        long ans = na + nb - nab;
		        System.out.println(ans);

	        }
	    }
	  public static long floorSum(long n, long m, long a, long b){
		  long ret = 0;
		  if(a >= m){
			  ret += (n - 1) * n * (a/m)/2;
			  a %= m;
		  }
		  if(b >= m){
			  ret += n * (b/m);
			  b %= m;
		  }
		  long ym = (a * n + b) / m;
		  long xm = (ym * m - b);
		  if(ym == 0) return ret;
		  ret += (n - (xm + a - 1)/a) * ym;
		  ret += floorSum(ym, a, m, (a - xm % a) % a);
		  return ret;
	  }

}
0