結果

問題 No.434 占い
ユーザー 37zigen37zigen
提出日時 2020-04-04 05:33:05
言語 Java21
(openjdk 21)
結果
AC  
実行時間 928 ms / 2,000 ms
コード長 1,399 bytes
コンパイル時間 2,656 ms
コンパイル使用メモリ 83,872 KB
実行使用メモリ 67,728 KB
最終ジャッジ日時 2023-09-16 05:26:04
合計ジャッジ時間 12,418 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
54,408 KB
testcase_01 AC 122 ms
55,744 KB
testcase_02 AC 124 ms
55,680 KB
testcase_03 AC 127 ms
55,796 KB
testcase_04 AC 127 ms
55,852 KB
testcase_05 AC 128 ms
56,124 KB
testcase_06 AC 226 ms
56,732 KB
testcase_07 AC 143 ms
56,380 KB
testcase_08 AC 154 ms
55,660 KB
testcase_09 AC 169 ms
55,980 KB
testcase_10 AC 171 ms
56,472 KB
testcase_11 AC 255 ms
59,496 KB
testcase_12 AC 356 ms
60,132 KB
testcase_13 AC 150 ms
55,756 KB
testcase_14 AC 159 ms
56,508 KB
testcase_15 AC 262 ms
59,104 KB
testcase_16 AC 243 ms
59,368 KB
testcase_17 AC 257 ms
60,080 KB
testcase_18 AC 308 ms
59,496 KB
testcase_19 AC 410 ms
60,888 KB
testcase_20 AC 928 ms
66,252 KB
testcase_21 AC 262 ms
59,468 KB
testcase_22 AC 242 ms
59,236 KB
testcase_23 AC 124 ms
55,836 KB
testcase_24 AC 241 ms
59,608 KB
testcase_25 AC 263 ms
59,180 KB
testcase_26 AC 201 ms
58,624 KB
testcase_27 AC 304 ms
59,628 KB
testcase_28 AC 314 ms
60,368 KB
testcase_29 AC 428 ms
61,376 KB
testcase_30 AC 870 ms
67,728 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%9,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=(int)((long)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;
				if(i!=n-1) {
					while(numerator%3==0) {
						numerator/=3;++c3;
					}
					while(denominator%3==0) {
						denominator/=3;--c3;
					}
					binom=mul(binom,numerator,inv(denominator));
				}
			}
			System.out.println(zero?0:(ans==0?9:ans));
		}
	}
	
	void tr(Object...objects) {System.out.println(Arrays.deepToString(objects));}
}

0