結果

問題 No.573 a^2[i] = a[i]
ユーザー kktykkty
提出日時 2017-11-04 12:08:29
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 58 ms / 2,000 ms
コード長 1,788 bytes
コンパイル時間 1,318 ms
コンパイル使用メモリ 147,848 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-15 02:36:48
合計ジャッジ時間 3,333 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

  #include <bits/stdc++.h>
  using namespace std;
  typedef long long ll;
  #define REP(i,n) FOR(i,0,n)
  #define FOR(i,a,b) for(ll i=a;i<b;i++)
  #define PB push_back
  #define LB lower_bound
  #define UB upper_bound
  #define PQ priority_queue
  #define UM unordered_map
  #define ALL(a) (a).begin(),(a).end()
  #define MOD 1000000007
  typedef vector<ll> vi;
  typedef vector<vector<ll>> vvi;
  const ll INF = (1ll << 30);
  typedef pair<ll,ll> pii;
  struct Edge{ ll s,t,c; };
  typedef vector<vector<Edge>> Graph;
  typedef vector<pii> vpii;

  pii extendedEuclid(ll a,ll b){
    bool swapped=false;
    if(a<b) {swap(a,b);swapped=true;}
    pii x={1,0},y={0,1};
    while(1){
      ll xv=a*x.first+b*x.second;
      ll yv=a*y.first+b*y.second;
      if(yv==0) {if(swapped) return {x.second,x.first}; else return x;}
      ll d=xv/yv;
      swap(x,y);
      y.first-=d*x.first;
      y.second-=d*x.second;
    }
  }

  ll inverse(ll a,ll mod) {
    return (mod+extendedEuclid(a,mod).first%mod)%mod;
  }

  ll combination(ll a,ll b,ll mod) {
    ll ret=1;
    for(ll i=0;i<b;i++) ret=ret*(a-i)%mod;
    for(ll i=1;i<=b;i++) ret=ret*inverse(i,mod)%mod;
    return ret;
  }

  vi combinations(ll a,ll mod) {
    vi ret(a+1);
    ret[0]=1;
    FOR(i,1,a+1) {
      ret[i]=ret[i-1];
      ret[i]=ret[i]*(a-i+1)%mod;
      ret[i]=ret[i]*inverse(i,mod)%mod;
    }
    return ret;
  }

  ll power(ll a, ll b, ll mod) {
    ll tmp=a;
    ll tmpp=1;
    ll ret=1;
    while(1){
      if(b%(tmpp*2)){
        ret=ret*tmp%mod;
        b-=tmpp;
      }
      if(b==0) break;
      tmp=tmp*tmp%mod;
      tmpp*=2;
    }
    return ret;
  }
  int main() {
    ll N; cin>>N;
    vi a=combinations(N,MOD);
    ll ans=0;
    FOR(k,1,N+1) ans=(ans+a[k]*power(k,N-k,MOD))%MOD;
    cout<<ans<<endl;

  }
0