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 - 055 - A&B&C

Posted at

AtCoder ABC 055 A&B&C

AtCoder - 055

A問題

1食800円で、15食食べる毎に200円もらえるらしい。
大食いチャレンジのようだ。
N食分の総額とチャレンジの賞金を計算する。

  • 総額は $食事×800$
  • 賞金は15食毎に200なので、$N/15×200$

        int numN = nextInt();

        int resX = numN * 800;
        int resY = (numN / 15) * 200;

        out.println(resX - resY);

B問題

i回トレーニングするたびにパワーがi倍になる。
つまり、N回トレーニングするとパワーがN!になる。
そして、$N! mod 10^9+7$

  • $N!$を生成
  • $N!$を$10^9+7$で割る

$N!$の時点でprimitiveだと桁あふれの予感しかしないのでBigDecimalで実装。


        int numN = nextInt();

        BigDecimal CONST = new BigDecimal(Math.pow(10, 9)).add(new BigDecimal("7"));
        BigDecimal res = BigDecimal.ONE;
        for (int i = 1; i <= numN; i++) {
            res = res.multiply(new BigDecimal(Integer.toString(i)));
        }

        out.println(res.remainder(CONST));

このコードはTLEになります

  • N!を計算している時点でNG

欲しいのは$mod$
ということは、各桁を$mod$していけばよい。
参考:余りについて


        int numN = nextInt();
        long CONST = (long) (Math.pow(10, 9) + 7);

        long res = 1;
        for (int i = 1; i <= numN; i++) {
            res = (res * i) % CONST;
        }

        out.println(res);

C問題

'S'と'C'を使って'Scc'を作る。
'C'×2で'S'が出来る。

  • 1S=2Cである。
  • Sと2Cを組み合わせてSが無くなったのちCが余った場合、4C消費すれば'Scc'を作れる。

        long numS = nextLong();
        long numC = nextLong();

        long res = 0;

Sの数を元に、'Scc'を作成


        long wk = numC - (numS * 2);

wk>=0ということはSを全て消費できたので、残りのCは「4Cで1Sccを作る」ために消費する。


        if (wk >= 0) {
            res = numS + (wk / 4);

wk<0ということはnumC/2よりもSの方が数が多いということ。

  • numS > numC/2

SをCに変換することは出来ないので、numC/2がそのまま'Scc'の数となる。

        } else {
            res = numC / 2;
        }
        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?