結果
問題 | No.613 Solitude by the window |
ユーザー |
![]() |
提出日時 | 2017-12-13 13:46:15 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 105 ms / 2,000 ms |
コード長 | 9,243 bytes |
コンパイル時間 | 1,091 ms |
コンパイル使用メモリ | 123,184 KB |
実行使用メモリ | 37,800 KB |
最終ジャッジ日時 | 2024-12-14 09:58:43 |
合計ジャッジ時間 | 3,964 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 21 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
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 = 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].num * 4 + res[1, 1].num * 2) - 2;Console.WriteLine((ans + ModInt.Mod) % ModInt.Mod);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 mainstatic 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 Exnamespace 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 Matrixpublic 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 ModNumberpublic partial struct ModInt {public static long Mod = (long)1e9 + 7;public long num;public ModInt(long n) : this() { num = n % Mod; if (num < 0) num += Mod; }public override string ToString() { return num.ToString(); }public static ModInt operator +(ModInt l, ModInt r) { var n = l.num + r.num; if (n >= Mod) n -= Mod; return new ModInt() { num = n }; }public static ModInt operator -(ModInt l, ModInt r) { var n = l.num + Mod - r.num; if (n >= Mod) n -= Mod; return new ModInt() { num = n }; }public static ModInt operator *(ModInt l, ModInt r) { return new ModInt(l.num * r.num); }public static implicit operator ModInt(long n) { return new ModInt(n); }public static ModInt Pow(ModInt v, long k) {ModInt ret = 1;var n = k;for (; n > 0; n >>= 1, v *= v) {if ((n & 1) == 1)ret = ret * v;}return ret;}}#endregionstruct 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 BabyStepGiantSteppublic 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