結果

問題 No.1706 Many Bus Stops (hard)
ユーザー harurunharurun
提出日時 2021-09-11 01:36:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,707 bytes
コンパイル時間 938 ms
コンパイル使用メモリ 82,032 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-30 09:29:45
合計ジャッジ時間 2,577 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 2 ms
4,380 KB
testcase_39 AC 1 ms
4,380 KB
testcase_40 AC 1 ms
4,380 KB
testcase_41 AC 1 ms
4,376 KB
testcase_42 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;

const long long mod=1000000007;

vector<vector<long long>> matrix_mul(const vector<vector<long long>>& A,const vector<vector<long long>>&B){
  vector<vector<long long>>C(A.size(),vector<long long>(B.at(0).size(),0));
  for(int i=0;i<A.size();i++){
    for(int k=0;k<B.size();k++){
      for(int j=0;j<B.at(0).size();j++){
        C.at(i).at(j)=(C.at(i).at(j)+A.at(i).at(k)*B.at(k).at(j))%mod;
      }
    }
  }
  return C;
}

vector<vector<long long>> matrix_exp(vector<vector<long long>> X,long long n){
  vector<vector<long long>>Y(X.size(),vector<long long>(X.size(),0));
  for(int i=0;i<X.size();i++){
    Y.at(i).at(i)=1;
  }
  while(n>0){
    if(n&1){
      Y=matrix_mul(Y,X);
    }
    X=matrix_mul(X,X);
    n>>=1;
  }
  return Y;
}

long long calculate_nth(long long n,vector<vector<long long>> A,long long C){
  A=matrix_exp(A,n);
  vector<vector<long long>> res;
  const vector<vector<long long>> b={
    {1},{0},{C},{0}
  };
  res=matrix_mul(A,b);
  return res.at(2).at(0);
}

long long mod_pow(long long x,long long n,long long m){
  long long res=1;
  while(n>0){
    if(n&1){
      res=res*x%m;
    }
    x=x*x%m;
    n>>=1;
  }
  return res;
}

void safe_mod(long long& x,long long m){
  while(x<0){
    x+=m;
  }
  x%=m;
  return;
}

int main(){
  long long C,N,M;
  cin>>C>>N>>M;
  const vector<vector<long long>>a={
    {{1,0,0,1},{0,1,C-1,C-2},{C,0,0,0},{0,C,0,0}}
  };
  long long ans,c,An,NAn,MNAn,MAn;
  ans=calculate_nth(N,a,C);
  c=mod_pow(mod_pow(C,mod-2,mod),N+1,mod);
  An=ans*c%mod;
  NAn=(1-An);
  safe_mod(NAn,mod);
  MNAn=mod_pow(NAn,M,mod);
  MAn=(1-MNAn);
  safe_mod(MAn,mod);
  cout<<MAn<<endl;
  return 0;  
}

0