結果

問題 No.434 占い
ユーザー 37zigen37zigen
提出日時 2020-04-04 05:18:21
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,301 bytes
コンパイル時間 2,606 ms
コンパイル使用メモリ 83,500 KB
実行使用メモリ 66,704 KB
最終ジャッジ日時 2023-09-16 05:25:16
合計ジャッジ時間 12,097 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 130 ms
55,668 KB
testcase_01 AC 128 ms
55,688 KB
testcase_02 AC 128 ms
55,808 KB
testcase_03 AC 126 ms
55,608 KB
testcase_04 AC 128 ms
55,636 KB
testcase_05 AC 128 ms
55,776 KB
testcase_06 AC 246 ms
56,972 KB
testcase_07 AC 145 ms
55,668 KB
testcase_08 AC 158 ms
55,960 KB
testcase_09 AC 164 ms
56,952 KB
testcase_10 AC 176 ms
56,296 KB
testcase_11 AC 270 ms
58,876 KB
testcase_12 AC 372 ms
60,464 KB
testcase_13 AC 156 ms
55,724 KB
testcase_14 AC 166 ms
56,412 KB
testcase_15 WA -
testcase_16 AC 259 ms
59,408 KB
testcase_17 AC 269 ms
57,840 KB
testcase_18 AC 313 ms
59,836 KB
testcase_19 AC 429 ms
61,052 KB
testcase_20 AC 995 ms
66,704 KB
testcase_21 WA -
testcase_22 AC 257 ms
59,056 KB
testcase_23 AC 129 ms
55,676 KB
testcase_24 AC 252 ms
59,348 KB
testcase_25 AC 263 ms
59,064 KB
testcase_26 AC 211 ms
58,656 KB
testcase_27 AC 315 ms
59,888 KB
testcase_28 AC 324 ms
59,944 KB
testcase_29 AC 449 ms
59,728 KB
testcase_30 AC 901 ms
66,084 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		new Main().run();
	}
	
	int[] toint(char[] cs) {
		int[] ret=new int[cs.length];
		for(int i=0;i<ret.length;++i)ret[i]=(int)(cs[i]-'0');
		return ret;
	}
	
	int pow(int a,int n) {
		int ret=1;
		for(;n>0;n>>=1,a=a*a%9)if(n%2==1)ret=ret*a%9;
		return ret;
	}
	
	int inv(int a) {
		if(a%3==0)throw new AssertionError();
		return pow(a,5);
	}
	
	int count3(int a) {
		int ret=0;
		while(a%3==0) {
			a/=3;
			++ret;
		}
		return ret;
	}
	
	int mul(int...a) {
		int r=1;
		for(int v:a)r=r*v%9;
		return r;
	}

	void run() {
		Scanner sc=new Scanner(System.in);
		int T=sc.nextInt();
		while(T-->0) {
			int[] a=toint(sc.next().toCharArray());
			int n=a.length;
			int binom=1;
			int c3=0;
			int ans=0;
			boolean zero=Arrays.stream(a).filter(v->v==0).count()==n;
			for(int i=0;i<n;++i) {
				if(c3<0)throw new AssertionError();
				ans+=mul(pow(3,c3),binom,a[i]);
				ans%=9;
				int numerator=n-1-i;
				int denominator=i+1;
				while(numerator>0&&numerator%3==0) {
					numerator/=3;++c3;
				}
				while(denominator>0&&denominator%3==0) {
					denominator/=3;--c3;
				}
				binom=mul(binom,numerator,inv(denominator));
			}
			System.out.println(zero?0:(ans==0?9:ans));
		}
	}
}

0