結果

問題 No.281 門松と魔法(1)
ユーザー 37zigen37zigen
提出日時 2020-04-01 18:33:53
言語 Java21
(openjdk 21)
結果
AC  
実行時間 122 ms / 1,000 ms
コード長 1,452 bytes
コンパイル時間 1,989 ms
コンパイル使用メモリ 74,472 KB
実行使用メモリ 58,064 KB
最終ジャッジ日時 2023-09-09 09:36:58
合計ジャッジ時間 11,258 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 116 ms
56,192 KB
testcase_01 AC 115 ms
55,436 KB
testcase_02 AC 118 ms
55,988 KB
testcase_03 AC 116 ms
55,352 KB
testcase_04 AC 117 ms
55,596 KB
testcase_05 AC 118 ms
56,004 KB
testcase_06 AC 117 ms
56,480 KB
testcase_07 AC 116 ms
56,176 KB
testcase_08 AC 116 ms
56,064 KB
testcase_09 AC 115 ms
55,656 KB
testcase_10 AC 119 ms
55,964 KB
testcase_11 AC 116 ms
56,080 KB
testcase_12 AC 116 ms
55,756 KB
testcase_13 AC 117 ms
56,024 KB
testcase_14 AC 116 ms
55,988 KB
testcase_15 AC 119 ms
55,692 KB
testcase_16 AC 118 ms
56,228 KB
testcase_17 AC 118 ms
55,868 KB
testcase_18 AC 115 ms
55,672 KB
testcase_19 AC 118 ms
55,632 KB
testcase_20 AC 116 ms
56,208 KB
testcase_21 AC 122 ms
56,156 KB
testcase_22 AC 119 ms
55,632 KB
testcase_23 AC 117 ms
55,880 KB
testcase_24 AC 120 ms
55,816 KB
testcase_25 AC 114 ms
56,260 KB
testcase_26 AC 115 ms
53,780 KB
testcase_27 AC 118 ms
56,012 KB
testcase_28 AC 117 ms
56,004 KB
testcase_29 AC 118 ms
55,680 KB
testcase_30 AC 118 ms
56,048 KB
testcase_31 AC 116 ms
55,928 KB
testcase_32 AC 116 ms
55,904 KB
testcase_33 AC 116 ms
58,064 KB
testcase_34 AC 117 ms
56,488 KB
testcase_35 AC 117 ms
56,108 KB
testcase_36 AC 117 ms
55,868 KB
testcase_37 AC 118 ms
55,784 KB
testcase_38 AC 118 ms
56,012 KB
testcase_39 AC 117 ms
56,332 KB
testcase_40 AC 116 ms
55,780 KB
testcase_41 AC 117 ms
55,696 KB
testcase_42 AC 117 ms
56,408 KB
testcase_43 AC 116 ms
56,040 KB
testcase_44 AC 113 ms
56,120 KB
testcase_45 AC 115 ms
56,240 KB
testcase_46 AC 116 ms
55,416 KB
testcase_47 AC 118 ms
55,560 KB
testcase_48 AC 117 ms
56,144 KB
testcase_49 AC 118 ms
55,780 KB
testcase_50 AC 117 ms
55,432 KB
testcase_51 AC 117 ms
55,980 KB
testcase_52 AC 117 ms
56,040 KB
testcase_53 AC 116 ms
55,924 KB
testcase_54 AC 117 ms
56,168 KB
testcase_55 AC 117 ms
55,764 KB
testcase_56 AC 118 ms
56,808 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

class Main {
	public static void main(String[] args) {
		new Main().run();
	}
	
	boolean check(long a,long b,long c) {
		return a>=0&&b>=0&&c>=0&&a!=b&&b!=c&&c!=a&&(b==Math.max(a, Math.max(b, c))||b==Math.min(a, Math.min(b, c)));
	}
	
	int max(long a,long b,long c,long d) {
		int ret=0;
		if(c>=b) {
			long q=(c+1-b+d-1)/d;
			ret+=q;
			c=Math.max(0,c-q*d);
		}
		if(a>=b) {
			long q=(a+1-b+d-1)/d;
			ret+=q;
			a=Math.max(0,a-q*d);
		}
		if(a==c) {
			ret++;
			c=Math.max(0, c-d);
		}
		if(check(a, b, c))return ret;
		else return -1;
	}
	
	int min(long a,long b,long c,long d) {
		int ret=0;
		if(a>c) {
			a^=c;c^=a;a^=c;
		}
		if(a==c) {
			ret++;
			a=Math.max(0, a-d);
		}
		if(a<=b) {
			long q=(b+1-a+d-1)/d;
			ret+=q;
			b=Math.max(0,b-q*d);
		}
		if(check(a, b, c))return ret;
		else return -1;
	}
	
	void run() {
		Scanner sc=new Scanner(System.in);
		long d=sc.nextLong();
		long a=sc.nextLong();
		long b=sc.nextLong();
		long c=sc.nextLong();
		if(d==0) {
			if(!check(a,b,c))System.out.println(-1);
			else System.out.println(0);
			return;
		}
		long INF=Long.MAX_VALUE/3;
		long ans=INF;
		if(min(a, b, c, d)!=-1)ans=Math.min(ans, min(a,b,c,d));
		if(max(a, b, c, d)!=-1)ans=Math.min(ans, max(a,b,c,d));
		System.out.println(ans==INF?-1:ans);
	}
	
	static void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

}

0