結果

問題 No.996 Phnom Penh
ユーザー nanaenanae
提出日時 2020-02-21 23:15:39
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 5,890 bytes
コンパイル時間 837 ms
コンパイル使用メモリ 103,660 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-06-22 05:29:32
合計ジャッジ時間 1,776 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 1 ms
6,812 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 1 ms
6,944 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;

enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;

void main() {
    string s;
    scan(s);

    auto n = s.length.to!int;

    long ans;

    for (int i = 0; i < n; i++) {
        if (i + 3 <= n && s[i .. i + 3] == "phn") {
            i += 3;
            int k;
            while (i + 2 <= n && s[i .. i + 2] == "om") {
                i += 2;
                k++;
            }
            ans += 1 + 2*k;
            continue;
        }
        if (i + 4 <= n && s[i .. i + 4] == "penh") {
            i += 4;
            int k;

            while (i + 2 <= n && s[i .. i + 2] == "om") {
                i += 2;
                k++;
            }
            ans += 2 + 2*k;
            continue;
        }
    }
    writeln(ans);
}


long[][] powMat(long[][] A, long x) {
    if (x > 0) {
        auto res = square(powMat(A, x>>1));
        if (x & 1) {
            res = mul(res, A);
        }
        return res;
    }
    else {
        auto res = new long[][](A.length, A.length);
        foreach (i ; 0 .. A.length) res[i][i] = 1;
        return res;
    }
}

long[][] square(long[][] A) {
    assert(A[0].length == A.length);
    auto B = new long[][](A.length, A.length);

    foreach (i ; 0 .. A.length) {
        foreach (j ; 0 .. A.length) {
            foreach (k ; 0 .. A.length) {
                B[i][j] += A[i][k] * A[k][j] % mod;
                B[i][j] %= mod;
            }
        }
    }

    return B;
}


auto mul(long[][] A, long[][] B) {
    assert(A[0].length == B.length);
    auto N = A.length;
    auto M = B[0].length;

    auto C = new long[][](N, M);

    foreach (i ; 0 .. N) {
        foreach (j ; 0 .. M) {
            foreach (k ; 0 .. A[i].length) {
                C[i][j] += A[i][k] * B[k][j] % mod;
                C[i][j] %= mod;
            }
        }
    }

    return C;
}

struct ModInt {
    long value = 0;
    long mod = 1_000_000_007L;

    this(long value, long mod = 1_000_000_007L) {
        this.mod = mod;
        this.value = (value % mod);
        if (this.value < 0) this.value += mod;
    }

    void opAssign(long x) {
        value = x % mod;
        if (value < 0) value += mod;
    }

    ModInt opBinary(string op)(const ModInt rhs){
        static if (op == "+") {
            auto lhs = this;
            lhs.value += rhs.value;
            if (lhs.value >= mod) lhs.value -= mod;
            return lhs;
        }
        static if (op == "-") {
            auto lhs = this;
            lhs.value -= rhs.value;
            if (lhs.value < 0) lhs.value += mod;
            return lhs;
        }
        static if (op == "*") {
            auto lhs = this;
            (lhs.value *= rhs.value) %= mod;
            return lhs;
        }
    }

    ModInt opBinary(string op)(long rhs){
        rhs %= mod;
        if (rhs < 0) rhs += mod;

        static if (op == "+") {
            auto lhs = this;
            lhs.value += rhs;
            if (lhs.value >= mod) lhs.value -= mod;
            return lhs;
        }
        static if (op == "-") {
            auto lhs = this;
            lhs.value -= rhs;
            if (lhs.value < 0) lhs.value += mod;
            return lhs;
        }
        static if (op == "*") {
            auto lhs = this;
            (lhs.value *= rhs) %= mod;
            return lhs;
        }
    }

    bool opEquals(ModInt a, ModInt b) {
        return a.value == b.value;
    }

    bool opEquals(T)(const T rhs) {
        long x = rhs % mod;
        if (x < 0) x += mod;
        return this.value == x;
    }

    void opOpAssign(string op)(const ModInt rhs) {
        static if (op == "+") {
            this.value += rhs.value;
            if (this.value >= mod) this.value -= mod;
        }
        static if (op == "-") {
            this.value -= rhs.value;
            if (this.value < 0) this.value += mod;
        }
        static if (op == "*") {
            (this.value *= rhs.value) %= mod;
        }
    }

    void opOpAssign(string op)(long rhs) {
        rhs %= mod;
        if (rhs < 0) rhs += mod;

        static if (op == "+") {
            this.value += rhs;
            if (this.value >= mod) this.value -= mod;
        }
        static if (op == "-") {
            this.value -= rhs;
            if (this.value < 0) this.value += mod;
        }
        static if (op == "*") {
            (this.value *= rhs) %= mod;
        }
    }

    string toString() {
        import std.conv : to;
        return value.to!string;
    }
}

unittest {
    enum mod = 1_000_000_007L;

    auto a = ModInt(2);
    auto b = ModInt(3);

    assert(a + b == 5);
    assert(a - b == mod - 1);
    assert(a * b == 6);

    assert(a + 8 == 10);
    assert(a - 1 == 1);
    assert(a * 7 == 14);

    ModInt c = 8;
    assert(c == 8);
}



void scan(T...)(ref T args) {
    import std.stdio : readln;
    import std.algorithm : splitter;
    import std.conv : to;
    import std.range.primitives;

    auto line = readln().splitter();
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}

void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}

bool chmin(T, U...)(ref T x, U args) {
    bool isChanged;

    foreach (arg; args) {
        if (x > arg) {
            x = arg;
            isChanged = true;
        }
    }

    return isChanged;
}

bool chmax(T, U...)(ref T x, U args) {
    bool isChanged;

    foreach (arg; args) {
        if (x < arg) {
            x = arg;
            isChanged = true;
        }
    }

    return isChanged;
}
0