結果

問題 No.74 貯金箱の退屈
ユーザー 37zigen37zigen
提出日時 2016-03-24 13:24:42
言語 Java21
(openjdk 21)
結果
AC  
実行時間 261 ms / 5,000 ms
コード長 3,237 bytes
コンパイル時間 2,250 ms
コンパイル使用メモリ 78,600 KB
実行使用メモリ 57,152 KB
最終ジャッジ日時 2024-04-09 23:29:33
合計ジャッジ時間 9,052 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
54,144 KB
testcase_01 AC 132 ms
53,980 KB
testcase_02 AC 123 ms
54,104 KB
testcase_03 AC 169 ms
54,480 KB
testcase_04 AC 162 ms
54,568 KB
testcase_05 AC 156 ms
54,220 KB
testcase_06 AC 154 ms
54,344 KB
testcase_07 AC 158 ms
54,212 KB
testcase_08 AC 176 ms
54,128 KB
testcase_09 AC 160 ms
54,064 KB
testcase_10 AC 164 ms
54,508 KB
testcase_11 AC 156 ms
54,368 KB
testcase_12 AC 155 ms
54,732 KB
testcase_13 AC 168 ms
54,352 KB
testcase_14 AC 261 ms
57,152 KB
testcase_15 AC 188 ms
54,264 KB
testcase_16 AC 198 ms
54,152 KB
testcase_17 AC 137 ms
54,228 KB
testcase_18 AC 129 ms
54,328 KB
testcase_19 AC 155 ms
54,228 KB
testcase_20 AC 187 ms
54,488 KB
testcase_21 AC 197 ms
54,272 KB
testcase_22 AC 113 ms
52,844 KB
testcase_23 AC 129 ms
54,184 KB
testcase_24 AC 183 ms
54,432 KB
testcase_25 AC 173 ms
54,100 KB
testcase_26 AC 193 ms
54,440 KB
testcase_27 AC 232 ms
55,212 KB
testcase_28 AC 189 ms
54,352 KB
testcase_29 AC 175 ms
54,120 KB
testcase_30 AC 218 ms
55,092 KB
testcase_31 AC 186 ms
54,316 KB
testcase_32 AC 158 ms
54,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder074;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
	public static void main(String[] args){
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int[] d=new int[n];
		int[] w=new int[n];//k番目のコインの裏表(0の時裏、1の時表)
		double[][] Coe=new double[n][n+1];
		for(int i=0;i<n;i++){
			d[i]=sc.nextInt();
		}
		for(int i=0;i<n;i++){
			w[i]=sc.nextInt();
		}
		for(int i=0;i<n;i++){
			Coe[(i+d[i])%n][i]=1;
			int s=i-d[i];
			while(s<0){
				s+=n;
			}
			Coe[(s)%n][i]=1;
		}
		for(int i=0;i<n;i++){
			int a=0;
			if(w[i]==1)a=0;
			if(w[i]==0)a=1;
			Coe[i][n]=a;
		}
		//		for(int o=0;o<Coe.length;o++){
		//			for(int p=0;p<Coe[0].length;p++){
		//				System.out.print(Coe[o][p]+" ");
		//			}
		//			System.out.println();
		//		}
		if(gaussElimination(Coe)==true){
			System.out.println("Yes");
		}else{
			System.out.println("No");
		}
	}
	public static boolean gaussElimination(double[][] coe){
		int min=Math.min(coe.length,coe[0].length-1);
		for(int i=0;i<min;i++){
			for(int j=i;j<min;j++){
				if(coe[j][i]!=0){
					double[] tmp=coe[j];
					coe[j]=coe[i];
					coe[i]=tmp;
					break;
				}
			}
			double coe1=coe[i][i];
			if(coe1==0){
				continue;
			}
			for(int j=0;j<coe[0].length;j++){
				coe[i][j]=coe[i][j]/coe1;
			}
			for(int j=0;j<coe.length;j++){
				if(i==j)continue;
				double coe2=coe[j][i];
				for(int k=0;k<coe[0].length;k++){
					coe[j][k]=(coe[j][k]-coe2*coe[i][k])%2;
					while(coe[j][k]<0){
						coe[j][k]+=2;
					}
				}
			}
		}
//		for(int i=0;i<coe.length;i++){
//			for(int j=0;j<coe[0].length;j++){
//				System.out.print(coe[i][j]+" ");
//			}
//			System.out.println();
//		}
		ArrayList<double[]> unfixed=new ArrayList<>(); 
//		double[] ans=new double[coe.length];
		if(coe[0].length-1<coe.length){
			for(int i=coe[0].length;i<coe.length;i++){
				if(coe[i][coe[0].length-2]==0&&coe[i][coe[0].length-1]!=0){
					return false;
				}else if(coe[i][coe[0].length-2]!=0){
					System.err.println("coe[i][coe[0].length-2]!=0");
					return false;
				}
			}
		}
		
		int count=0;
		int min1=Math.min(coe[0].length-1, coe.length);
		for(int i=0;i<min;i++){
			if(coe[i][i]==0){
				unfixed.add(coe[i]);
				count++;
			}
			if(coe[coe.length-1][coe[0].length-2]==0&&coe[coe.length-1][coe[0].length-1]!=0){
				return false;
			}
		}
		if(count==0)return true;
		double[][] a=new double[unfixed.size()][coe[0].length-1];
		
		for(int i=0;i<unfixed.size();i++){
			double[] b=unfixed.get(i);
			for(int j=0;j<coe[0].length-1;j++){
				a[i][j]=b[j+1];
			}
		}
		return gaussElimination(a);
		
//		for(int i=coe.length-1;i>=0;i--){
//			double sum=0;
//			for(int j=i+1;j<coe[0].length-1;j++){
//				sum=(sum+coe[i][j]*ans[j])%2;
//			}
//			if(coe[i][i]==1){
//				ans[i]=(-sum+coe[i][coe[0].length-1])%2;
//				while(ans[i]<0)ans[i]+=2;
//			}else if(coe[i][i]==0){
//				unfixed.add(coe[i]);
//				
//			}else{
//				System.err.println("ErrorAt90");
//			}
//		}
	
//		System.out.println("answer");
//		for(int i=0;i<coe.length;i++){
//			System.out.print(ans[i]+" ");
//		}
//		for(int i=0;i<coe.length;i++){
//			if((int)ans[i]!=ans[i]){
//				return false;
//			}
//		}
//		System.out.println();
	}
}

0