0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ABC - 065- A&B&C

Posted at

AtCoder ABC 065 B&C

AtCoder - 065

A問題

賞味期限のA日前に食品を買ってきたので、$A=購入日$

  • 購入日>買ってきてからB日後
    • delicious
  • 購入日+賞味期限をX日過ぎている>買ってきてからB日後
    • safe
  • 購入日+賞味期限をX日過ぎている<買ってきてからB日後
    • dangerous
	private void solveA() {
		int numX = nextInt();
		int numA = nextInt();
		int numB = nextInt();

		if (numA >= numB) {
			out.println("delicious");
		} else if (numA + numX >= numB) {
			out.println("safe");
		} else if (numA + numX < numB) {
			out.println("dangerous");
		}

	}

B問題

  • 必ず1番目(配列の0番目 {$a_o$})からスタート
  • {$a_0$}で指定された{$a_i$}を確認して次の{$a_n$}を見る
  • loopして、2を見つけたら終了(2を見つけたときにもボタンを押す必要はある)
  • ただし、$ボタンを押した回数>配列の個数$までいったら、ボタン同士がloopしているので2にはたどり着けないと判定
    • {$3,2,1$}のようなな配列は双方向なので簡単だが、{$3,2,4,1$}のような配列は何個か経由してloopするため回数で判定している
	private void solveB() {
		int numN = nextInt();
		int[] wk = IntStream.range(0, numN).map(i -> nextInt()).toArray();

		int res = chkB(wk, 1, 0, 0);

		out.println(res);
	}

	private int chkB(int[] wk, int currentI, int total, int buttonCnt) {

		if (buttonCnt > wk.length) {
			return -1;
		}
		int index = wk[currentI - 1];
		if (index == 2) {
			return total + 1;
		} else {
			return chkB(wk, index, total + 1, buttonCnt + 1);
		}

	}

C問題

偶数の場合と奇数の場合で場合わけ

  • そもそも、犬犬猿や犬猿猿のような組み合わせは認められないので、犬と猿の差分は$\leqq1$出ないといけない。ありえるパターンは4つ。
    • 犬猿犬猿犬猿犬(合計が奇数)
    • 猿犬猿犬猿犬猿(合計が奇数)
    • 猿犬猿犬猿犬(合計が偶数)
    • 犬猿犬猿犬猿(合計が偶数)
  • $(3,2)$匹の組み合わせの場合は、$(3 * 2) * (2 * 1)$となる
  • $(2,2)$匹の組み合わせの場合は、$(2 * 2) * (1 * 1)$となる
    • ただし、$(1 * 1)$の時は、$(A,B),(B,A)$という二つの組み合わせがあるのに注意
	/**
	 * 3 , 2
	 * (3 * 2) * (2 * 1)
	 *
	 * 2 , 2
	 * (2 * 2) * (1 * 1)
	 */
	private void solveC() {
		long numN = nextInt();
		long numM = nextInt();
		long diffNum = Math.abs(numN - numM);
		//犬と猿の差分が2以上の場合は犬犬、猿猿の組み合わせが発生する
		if (diffNum > 1) {
			out.println(0);
			return;
		}

		//両方1なら組み合わせは二通り
		if (numN == 1 && numM == 1) {
			out.println(2);
			return;
		}

		long CONST = (long) (Math.pow(10, 9) + 7);
		long res = 1;
		long big = numN > numM ? numN : numM;
		long small = numN > numM ? numM : numN;

		while (small >= 1) {
			if (small != 1) {
				res *= (big * small) % CONST;
			} else {
				//1*1は組み合わせとしては2通り
				res *= 2;
			}
			//modしておかないと溢れる
			res = res % CONST;
			big--;
			small--;
		}
		out.println(res);

	}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?