結果

問題 No.613 Solitude by the window
ユーザー 紙ぺーぱー紙ぺーぱー
提出日時 2017-12-13 13:42:23
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 9,943 bytes
コンパイル時間 1,312 ms
コンパイル使用メモリ 123,812 KB
実行使用メモリ 36,200 KB
最終ジャッジ日時 2024-12-14 09:57:42
合計ジャッジ時間 4,126 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
26,420 KB
testcase_01 AC 31 ms
26,480 KB
testcase_02 AC 92 ms
31,504 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 AC 92 ms
35,468 KB
testcase_12 AC 90 ms
33,300 KB
testcase_13 AC 92 ms
33,700 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 32 ms
26,348 KB
testcase_18 AC 30 ms
26,468 KB
testcase_19 WA -
testcase_20 AC 32 ms
26,348 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.Linq;
using Debug = System.Diagnostics.Debug;
//using System.Numerics;
//using static System.Math;
using Number = ModInt;
namespace Program {
    public class Solver {

        public void Solve() {

            long n = rl;
            ModInt.Mod = ri;
            //if (ModInt.Mod == 2) { Console.WriteLine(0); return; }

            //var primes = new List<int>();
            //MathEx.Sieve(100000, primes);
            //foreach (var x in primes) 
            {
                //ModInt.Mod = x;
                var mat = new Matrix(2, 2);
                mat[0, 0] = 4; mat[0, 1] = -1;
                mat[1, 0] = 1;
                var E = new Matrix(2, 2); E[0, 0] = E[1, 1] = 1;
                var nmod = (int)MathEx.BabyStepGiantStep(mat, E);

                //Debug.WriteLine(nmod);
                Swap(ref nmod, ref ModInt.Mod);
                var k = ModInt.Pow(2, n).num;
                Swap(ref nmod, ref ModInt.Mod);
                var res = Matrix.Pow(mat, k);
                var ans = (res[1, 0] * 4 + res[1, 1] * 2) - 2;
                Console.WriteLine(ans);


                dbg(Matrix.Pow(mat, nmod));
            }
        }
        void dbg(Matrix m) {
            if (m[0, 0].num != 1) throw new Exception();
            if (m[0, 1].num != 0) throw new Exception();
            if (m[1, 0].num != 0) throw new Exception();
            if (m[1, 1].num != 1) throw new Exception();
        }
        const long INF = 1L << 60;
        int[] dx = { 1, 0, -1, 0 };
        int[] dy = { 0, 1, 0, -1 };
        //*
        int ri { get { return sc.Integer(); } }
        long rl { get { return sc.Long(); } }
        double rd { get { return sc.Double(); } }
        string rs { get { return sc.Scan(); } }
        //*/

        public IO.StreamScanner sc = new IO.StreamScanner(Console.OpenStandardInput());
        static T[] Enumerate<T>(int n, Func<int, T> f) { var a = new T[n]; for (int i = 0; i < n; ++i) a[i] = f(i); return a; }
        static public void Swap<T>(ref T a, ref T b) { var tmp = a; a = b; b = tmp; }
    }
}

#region main
static class Ex {
    static public string AsString(this IEnumerable<char> ie) { return new string(ie.ToArray()); }
    static public string AsJoinedString<T>(this IEnumerable<T> ie, string st = " ") {
        return string.Join(st, ie.Select(x => x.ToString()).ToArray());
        //return string.Join(st, ie);
    }
    static public void Main() {
        Console.SetOut(new System.IO.StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
        var solver = new Program.Solver();
        solver.Solve();
        Console.Out.Flush();
    }
}
#endregion
#region Ex
namespace Program.IO {
    using System.Globalization;
    using System.IO;
    using System.Text;
    public class Printer: StreamWriter {
        public override IFormatProvider FormatProvider { get { return CultureInfo.InvariantCulture; } }
        public Printer(Stream stream) : base(stream, new UTF8Encoding(false, true)) { }
    }
    public class StreamScanner {
        public StreamScanner(Stream stream) { str = stream; }
        public readonly Stream str;
        private readonly byte[] buf = new byte[1024];
        private int len, ptr;
        public bool isEof = false;
        public bool IsEndOfStream { get { return isEof; } }
        private byte read() {
            if (isEof) return 0;
            if (ptr >= len) { ptr = 0; if ((len = str.Read(buf, 0, 1024)) <= 0) { isEof = true; return 0; } }
            return buf[ptr++];
        }
        public char Char() { byte b = 0; do b = read(); while ((b < 33 || 126 < b) && !isEof); return (char)b; }

        public string Scan() {
            var sb = new StringBuilder();
            for (var b = Char(); b >= 33 && b <= 126; b = (char)read())
                sb.Append(b);
            return sb.ToString();
        }
        public string ScanLine() {
            var sb = new StringBuilder();
            for (var b = Char(); b != '\n' && b != 0; b = (char)read())
                if (b != '\r') sb.Append(b);
            return sb.ToString();
        }
        public long Long() { return isEof ? long.MinValue : long.Parse(Scan()); }
        public int Integer() { return isEof ? int.MinValue : int.Parse(Scan()); }
        public double Double() { return isEof ? double.NaN : double.Parse(Scan(), CultureInfo.InvariantCulture); }
    }
}
#endregion
#region Matrix
public class Matrix {
    int row, col;
    public Number[] mat;
    public Number this[int r, int c] {
        get { return mat[r * col + c]; }
        set { mat[r * col + c] = value; }
    }
    public Matrix(int r, int c) {
        row = r; col = c;
        mat = new Number[row * col];
    }
    public static Matrix operator +(Matrix l, Matrix r) {
        check(l, r);
        var ret = new Matrix(l.row, l.col);
        for (int i = 0; i < l.row; i++)
            for (int j = 0; j < l.col; j++)
                ret.mat[i * ret.col + j] = l.mat[i * l.col + j] + r.mat[i * r.col + j];
        return ret;

    }
    public static Matrix operator *(Matrix l, Matrix r) {
        checkMul(l, r);
        var ret = new Matrix(l.row, r.col);
        for (int i = 0; i < l.row; i++)
            for (int k = 0; k < l.col; k++)
                for (int j = 0; j < r.col; j++)
                    ret.mat[i * r.col + j] = (ret.mat[i * r.col + j] + l.mat[i * l.col + k] * r.mat[k * r.col + j]);
        return ret;
    }
    public static Matrix Pow(Matrix m, long n) {
        var ret = new Matrix(m.row, m.col);
        for (int i = 0; i < m.row; i++)
            ret.mat[i * m.col + i] = 1;
        for (; n > 0; m *= m, n >>= 1)
            if ((n & 1) == 1)
                ret = ret * m;
        return ret;

    }
    public static Matrix Trans(Matrix m) {
        var ret = new Matrix(m.col, m.row);
        for (int i = 0; i < m.row; i++)
            for (int j = 0; j < m.col; j++)
                ret.mat[j * m.col + i] = m.mat[i * m.col + j];
        return ret;
    }
    [System.Diagnostics.Conditional("DEBUG")]
    static private void check(Matrix a, Matrix b) {
        if (a.row != b.row || a.col != b.col)
            throw new Exception("row and col have to be same.");

    }
    [System.Diagnostics.Conditional("DEBUG")]
    static private void checkMul(Matrix a, Matrix b) {
        if (a.col != b.row)
            throw new Exception("row and col have to be same.");
    }
    public Number[][] Items {
        get {
            var a = new Number[row][];
            for (int i = 0; i < row; i++) {
                a[i] = new Number[col];
                for (int j = 0; j < col; j++)
                    a[i][j] = mat[i * col + j];
            }
            return a;
        }
    }
    public override string ToString() {
        return string.Format("{0}*{1}", row, col);
    }
}
#endregion

//剰余環
#region ModInt
public struct ModInt {
    public static int Mod = (int)1e9 + 7;
    public int num;
    public ModInt(int n) { num = n; }
    public override string ToString() { return num.ToString(); }
    public static ModInt operator +(ModInt l, ModInt r) { l.num += r.num; if (l.num >= Mod) l.num -= Mod; return l; }
    public static ModInt operator -(ModInt l, ModInt r) { l.num -= r.num; if (l.num < 0) l.num += Mod; return l; }
    public static ModInt operator *(ModInt l, ModInt r) { return new ModInt((int)(l.num * (long)r.num % Mod)); }
    public static implicit operator ModInt(int n) { n %= Mod; if (n < 0) n += Mod; return new ModInt(n); }
    public static implicit operator ModInt(long n) { n %= Mod; if (n < 0) n += Mod; return new ModInt((int)n); }
    public static ModInt Pow(ModInt v, long k) { return Pow(v.num, k); }
    public static ModInt Pow(int v, long k) {
        int ret = 1;
        for (k %= Mod - 1; k > 0; k >>= 1, v = (int)(v * (long)v % Mod))
            if ((k & 1) == 1) ret = (int)(ret * (long)v % Mod);
        return new ModInt(ret);
    }
    public static ModInt Inverse(ModInt v) { return Pow(v, Mod - 2); }
}
#endregion

struct cmp: IComparer<Number[]> {
    public int Compare(Number[] x, Number[] y) {
        for (int i = 0; i < x.Length; i++)
            if (x[i].num < y[i].num) return -1;
            else if (x[i].num > y[i].num) return 1;
        return 0;
    }
}

#region SortedMap<K,V>
public class SortedMap<K, V>: SortedDictionary<K, V> {

    public SortedMap() {

    }
    public SortedMap(IComparer<K> cmp) : base(cmp) { }

    new public V this[K i] {
        get {
            V v;
            return TryGetValue(i, out v) ? v : base[i] = default(V);
        }
        set { base[i] = value; }
    }
}
#endregion


#region BabyStepGiantStep
public partial class MathEx {
    static public long BabyStepGiantStep(Matrix x, Matrix y) {
        var H = (long)Math.Sqrt(ModInt.Mod) + 1;
        var baby = new Number[H][];
        var dic = new SortedMap<Number[], int>(default(cmp));
        var xby = y;
        for (long b = 0; b < H; b++) {
            baby[b] = xby.mat.ToArray();
            xby = x * xby;
        }
        for (int i = 0; i < H; i++)
            dic[baby[i]] = i;
        var xH = Matrix.Pow(x, H);
        var xaH = xH;
        for (long a = 1; a <= H; a++) {
            int res;
            if (dic.TryGetValue(xaH.mat, out res))
                return a * H - res;

            xaH *= xH;
        }

        return -1;
    }

}
#endregion


#region sieve O(NloglogN)
static public partial class MathEx {
    static public bool[] Sieve(int p, List<int> primes = null) {
        var isPrime = new bool[p + 1];
        for (int i = 2; i < isPrime.Length; i++) isPrime[i] = true;
        for (int i = 2; i * i <= p; i++)
            if (!isPrime[i]) continue;
            else for (int j = i * i; j <= p; j += i) isPrime[j] = false;
        if (primes != null) for (int i = 0; i <= p; i++) if (isPrime[i]) primes.Add(i);

        return isPrime;
    }
}
#endregion
0