結果

問題 No.2365 Present of good number
ユーザー 👑 kakel-sankakel-san
提出日時 2023-08-17 12:13:22
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 67 ms / 2,000 ms
コード長 3,670 bytes
コンパイル時間 1,282 ms
コンパイル使用メモリ 66,572 KB
実行使用メモリ 23,932 KB
最終ジャッジ日時 2023-08-17 12:13:28
合計ジャッジ時間 5,568 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 63 ms
23,744 KB
testcase_01 AC 63 ms
21,676 KB
testcase_02 AC 64 ms
21,916 KB
testcase_03 AC 65 ms
21,920 KB
testcase_04 AC 64 ms
21,696 KB
testcase_05 AC 64 ms
21,848 KB
testcase_06 AC 65 ms
21,700 KB
testcase_07 AC 65 ms
21,624 KB
testcase_08 AC 65 ms
21,884 KB
testcase_09 AC 64 ms
21,916 KB
testcase_10 AC 64 ms
21,760 KB
testcase_11 AC 66 ms
19,832 KB
testcase_12 AC 64 ms
19,796 KB
testcase_13 AC 64 ms
23,932 KB
testcase_14 AC 65 ms
23,816 KB
testcase_15 AC 67 ms
23,752 KB
testcase_16 AC 65 ms
21,860 KB
testcase_17 AC 65 ms
21,856 KB
testcase_18 AC 65 ms
21,784 KB
testcase_19 AC 66 ms
23,932 KB
testcase_20 AC 65 ms
21,848 KB
testcase_21 AC 64 ms
21,884 KB
testcase_22 AC 64 ms
21,772 KB
testcase_23 AC 66 ms
21,744 KB
testcase_24 AC 65 ms
21,776 KB
testcase_25 AC 65 ms
21,904 KB
testcase_26 AC 65 ms
23,868 KB
testcase_27 AC 65 ms
21,756 KB
testcase_28 AC 65 ms
21,772 KB
testcase_29 AC 65 ms
21,792 KB
testcase_30 AC 65 ms
21,740 KB
testcase_31 AC 62 ms
19,808 KB
testcase_32 AC 63 ms
23,808 KB
testcase_33 AC 65 ms
23,732 KB
testcase_34 AC 64 ms
21,980 KB
testcase_35 AC 64 ms
21,736 KB
testcase_36 AC 63 ms
19,764 KB
testcase_37 AC 65 ms
21,908 KB
testcase_38 AC 66 ms
21,740 KB
testcase_39 AC 64 ms
23,824 KB
testcase_40 AC 64 ms
23,740 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;
class Program
{
    static int NN => int.Parse(ReadLine());
    static long[] NList => ReadLine().Split().Select(long.Parse).ToArray();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var c = NList;
        var (n, k) = (c[0], c[1]);
        WriteLine(Good(n, k));
    }
    static long Good(long n, long k)
    {
        var mod = 1_000_000_007;
        var p = PDiv(n);
        while (p.Count != (p.ContainsKey(2) ? 1 : 0) + (p.ContainsKey(3) ? 1 : 0))
        {
            var np = new Dictionary<long, long>();
            foreach (var kv in p)
            {
                var nkey = PDiv(kv.Key + 1);
                foreach (var nkv in nkey)
                {
                    if (np.ContainsKey(nkv.Key)) np[nkv.Key] += kv.Value * nkv.Value;
                    else np[nkv.Key] = kv.Value * nkv.Value;
                }
            }
            --k;
            p = np;
            if (k == 0)
            {
                var ans2 = 1L;
                foreach (var kv in p) for (var i = 0; i < kv.Value; ++i) ans2 = ans2 * kv.Key % mod;
                return ans2;
            }
        }
        var m = new Matrix(mod - 1);
        var a = new long[][] { new long[] { p.ContainsKey(2) ? p[2] : 0, p.ContainsKey(3) ? p[3] : 0 }};
        var x = new long[][] { new long[] { 0, 1 }, new long[] { 2, 0 }};
        var b = m.Mul(a, m.Pow(x, k));
        return Exp(2, b[0][0], mod) * Exp(3, b[0][1], mod) % mod;
    }
    static long Exp(long n, long k, int mod)
    {
        if (k == 0) return 1;
        if (k == 1) return n % mod;
        var half = Exp(n, k / 2, mod);
        var result = (half * half) % mod;
        return ((k % 2) == 0) ? result : ((result * n) % mod);
    }
    static Dictionary<long, long> PDiv(long n)
    {
        var dic = new Dictionary<long, long>();
        var tmp = n;
        while (tmp % 2 == 0)
        {
            tmp /= 2;
            if (dic.ContainsKey(2)) ++dic[2];
            else dic.Add(2, 1);
        }
        for (var p = 3L; p * p <= n; p += 2)
        {
            while (tmp % p == 0)
            {
                tmp /= p;
                if (dic.ContainsKey(p)) ++dic[p];
                else dic.Add(p, 1);
            }
            if (tmp == 1) break;
        }
        if (tmp > 1) dic.Add(tmp, 1);
        return dic;
    }
    class Matrix
    {
        int mod;
        // 行列の累乗
        public Matrix(int _mod = 1_000_000_007)
        {
            mod = _mod;
        }
        public long[][] Pow(long[][] m, long k)
        {
            var multi = m;
            var r = new long[m.Length][];
            for (var i = 0; i < m.Length; ++i)
            {
                r[i] = new long[m.Length];
                r[i][i] = 1;
            }
            while (k > 0)
            {
                if ((k & 1) == 1) r = Mul(r, multi);
                multi = Mul(multi, multi);
                k >>= 1;
            }
            return r;
        }
        // 行列の積
        public long[][] Mul(long[][] x, long[][] y)
        {
            var r = new long[x.Length][];
            for (var i = 0; i < x.Length; ++i) r[i] = new long[y[0].Length];
            for (var i = 0; i < x.Length; ++i) for (var k = 0; k < x[0].Length; ++k)
            {
                for (var j = 0; j < y[0].Length; ++j) r[i][j] = (r[i][j] + x[i][k] * y[k][j]) % mod;
            }
            return r;
        }
    }
}
0