結果

問題 No.391 CODING WAR
ユーザー suikameron1suikameron1
提出日時 2018-10-28 19:29:30
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 341 ms / 2,000 ms
コード長 2,347 bytes
コンパイル時間 5,039 ms
コンパイル使用メモリ 105,096 KB
実行使用メモリ 24,380 KB
最終ジャッジ日時 2023-08-12 08:08:35
合計ジャッジ時間 6,290 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 57 ms
22,888 KB
testcase_01 AC 57 ms
22,740 KB
testcase_02 AC 59 ms
20,748 KB
testcase_03 AC 58 ms
22,832 KB
testcase_04 AC 57 ms
20,696 KB
testcase_05 AC 57 ms
20,748 KB
testcase_06 AC 56 ms
20,728 KB
testcase_07 AC 57 ms
20,688 KB
testcase_08 AC 58 ms
20,788 KB
testcase_09 AC 341 ms
24,296 KB
testcase_10 AC 317 ms
24,380 KB
testcase_11 AC 183 ms
22,280 KB
testcase_12 AC 57 ms
20,692 KB
testcase_13 AC 327 ms
22,272 KB
testcase_14 AC 272 ms
21,936 KB
testcase_15 AC 306 ms
22,108 KB
testcase_16 AC 201 ms
23,544 KB
testcase_17 AC 229 ms
21,752 KB
testcase_18 AC 175 ms
21,392 KB
testcase_19 AC 175 ms
23,440 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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.Linq;//リストの使用
using System.Collections.Generic;
class Program
{
static string[] input = Console.ReadLine().Split(' ');//Splitで区切り文字を指定して複数個受け取る。
static long n = long.Parse(input[0]);
static long k = long.Parse(input[1]);
static long p = 1000000007;
static long answer = 0;
static long[] factorials = new long[k+1];//i!(mod p)を先にメモ
static long[] factorialRs = new long[k+1];//i!^-1(mod p), pは素数
 
    static void Main()
    {
    factorials[0] = 1;
    factorialRs[k] = DivideModFactorial(k,p);
    for(long i = 1; i <= k; i++)
    {
      factorials[i] = (factorials[i-1]*i)%p;//i!(mod p)
      factorialRs[k-i] = (factorialRs[k+1-i]*(k+1-i))%p;//逆元も先にメモ
    }
   
    if(k > n) answer = 0;
    else
    {
      for(long i = 0; i <= k-1; i++)
      {
        long memo = Comb(k, i, p) * DivideMod(k-i, n, p);
        if(i % 2 == 0)
        {
          answer += memo;
          answer %= p;
        }else
        {
          answer -= memo;
          answer %= p;
          if(answer < 0) answer += p;
        }
      }
    }
 
        Console.WriteLine(answer);//WriteLineをWriteとすると、改行なしで出力。
    }
 
  static long DivideMod(long x, long a, long p)//戻り値はx^a(mod p)
  {
    long num = 2;
    long answer = 1;
    long check = a;
    long memo = x%p;
     
    while(check > 0)
    {
      if(check % num == num / 2)
      {
        check -= num / 2;
        answer *= memo;
        answer %= p;
      }
    memo *= memo;
    memo %= p;
    num *= 2;
    }
    return answer;
  }
 
  static long DivideModReverse(long x, long p)//戻り値はx^-1(mod p), pは素数
  {
    long answer = DivideMod(x, p-2, p);
    return answer;
  }
 
  static long DivideModFactorial(long x, long p)//戻り値はx!^-1(mod p), pは素数
  {
    long answer = 1;
    for(long i = x; i >= 2; i--)
    {
      answer *= DivideModReverse(i, p);
      answer %= p;
    }
    return answer;
  }
 
  static long Comb(long a, long b, long p)//戻り値は組み合わせC(a,b)のmod p
  {
    long answer = 1;
    answer *= factorials[a];
    answer %= p;
    answer *= factorialRs[a-b];//引数a-bは負にならないようにする
    answer %= p;
    answer *= factorialRs[b];
    answer %= p;
    return answer;
  }
 
   
}
0