結果
問題 | No.117 組み合わせの数 |
ユーザー | AreTrash |
提出日時 | 2018-03-15 11:31:18 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 329 ms / 5,000 ms |
コード長 | 5,521 bytes |
コンパイル時間 | 4,880 ms |
コンパイル使用メモリ | 109,824 KB |
実行使用メモリ | 36,992 KB |
最終ジャッジ日時 | 2024-05-09 13:19:25 |
合計ジャッジ時間 | 2,534 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
using System; using System.IO; using System.Collections.Generic; namespace No117 { public class Program { readonly ModInt[] facts = new ModInt[2000010]; void Solve(StreamScanner ss, StreamWriter sw) { //--------------------------------- facts[0] = 1; for (var i = 1; i <= 2000000; i++) { facts[i] = facts[i - 1] * i; } var T = ss.Next(Int); for (var i = 0; i < T; i++) { var S = ss.Next(String); var Q = S[0]; var sp = Array.ConvertAll(S.Substring(1).Trim('(', ')').Split(','), int.Parse); var N = sp[0]; var K = sp[1]; if (Q == 'C') sw.WriteLine(NCK(N, K)); if (Q == 'P') sw.WriteLine(NPK(N, K)); if (Q == 'H') sw.WriteLine(NHK(N, K)); } //--------------------------------- } ModInt NCK(int n, int k) { if (n < k) return 0; return facts[n] / facts[k] / facts[n - k]; } ModInt NPK(int n, int k) { if (n < k) return 0; return facts[n] / facts[n - k]; } ModInt NHK(int n, int k) { if (k == 0) return 1; if (n == 0) return 0; return facts[n + k - 1] / facts[k] / facts[n - 1]; } static void Main() { var ss = new StreamScanner(new StreamReader(Console.OpenStandardInput())); var sw = new StreamWriter(Console.OpenStandardOutput()) {AutoFlush = false}; new Program().Solve(ss, sw); sw.Flush(); } static readonly Func<string, string> String = s => s; static readonly Func<string, int> Int = int.Parse; static readonly Func<string, long> Long = long.Parse; static readonly Func<string, double> Double = double.Parse; } public static partial class MathX { public static int GcdEx(int a, int b, out int x, out int y) { if (b == 0) { x = 1; y = 0; return a; } int tx, ty; var gcd = GcdEx(b, a % b, out tx, out ty); x = ty; y = tx - ty * (a / b); return gcd; } } public struct ModInt { public static int Mod = 1000000007; readonly long value; public int Value { get { return (int)value; } } public ModInt Inverse { get { return GetInverse(); } } public ModInt(long value) { value %= Mod; this.value = value < 0 ? value + Mod : value; } ModInt GetInverse() { int x, y; if (MathX.GcdEx(Value, Mod, out x, out y) != 1) { throw new InvalidOperationException("'Value' and 'Mod' must be disjoint."); } return x; } public ModInt Square() { return this * this; } public ModInt Pow(long exp) { if (exp == 0) return 1; if (exp % 2 == 0) return Pow(exp / 2).Square(); return this * Pow(exp - 1); } public static ModInt Parse(string str) { return long.Parse(str); } public static implicit operator ModInt(long value) { return new ModInt(value); } public static ModInt operator +(ModInt left, ModInt right) { return left.value + right.value; } public static ModInt operator -(ModInt left, ModInt right) { return left.value - right.value; } public static ModInt operator *(ModInt left, ModInt right) { return left.value * right.value; } public static ModInt operator /(ModInt left, ModInt right) { return left * right.Inverse; } public override bool Equals(object obj) { if (!(obj is ModInt)) return false; return value == ((ModInt)obj).value; } public override int GetHashCode() { return value.GetHashCode(); } public override string ToString() { return value.ToString(); } } public class StreamScanner { static readonly char[] Sep = {' '}; readonly Queue<string> buffer = new Queue<string>(); readonly TextReader textReader; public StreamScanner(TextReader textReader) { this.textReader = textReader; } public T Next<T>(Func<string, T> parser) { if (buffer.Count != 0) return parser(buffer.Dequeue()); var nextStrings = textReader.ReadLine().Split(Sep, StringSplitOptions.RemoveEmptyEntries); foreach (var nextString in nextStrings) buffer.Enqueue(nextString); return Next(parser); } public T[] Next<T>(Func<string, T> parser, int x) { var ret = new T[x]; for (var i = 0; i < x; ++i) ret[i] = Next(parser); return ret; } public T[][] Next<T>(Func<string, T> parser, int x, int y) { var ret = new T[y][]; for (var i = 0; i < y; ++i) ret[i] = Next(parser, x); return ret; } } }