結果
| 問題 |
No.74 貯金箱の退屈
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-03-24 13:24:42 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 239 ms / 5,000 ms |
| コード長 | 3,237 bytes |
| コンパイル時間 | 2,534 ms |
| コンパイル使用メモリ | 78,464 KB |
| 実行使用メモリ | 43,184 KB |
| 最終ジャッジ日時 | 2024-10-02 00:09:15 |
| 合計ジャッジ時間 | 8,816 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 30 |
ソースコード
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();
}
}