結果

問題 No.645 Count Permutation
ユーザー rickythetarickytheta
提出日時 2018-02-02 22:53:43
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 210 ms / 2,000 ms
コード長 1,983 bytes
コンパイル時間 1,301 ms
コンパイル使用メモリ 153,592 KB
実行使用メモリ 54,928 KB
最終ジャッジ日時 2023-08-30 02:24:30
合計ジャッジ時間 4,790 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
5,764 KB
testcase_01 AC 19 ms
5,836 KB
testcase_02 AC 20 ms
6,176 KB
testcase_03 AC 20 ms
5,948 KB
testcase_04 AC 19 ms
5,776 KB
testcase_05 AC 19 ms
5,708 KB
testcase_06 AC 19 ms
5,768 KB
testcase_07 AC 19 ms
5,896 KB
testcase_08 AC 20 ms
5,732 KB
testcase_09 AC 20 ms
6,092 KB
testcase_10 AC 20 ms
5,916 KB
testcase_11 AC 26 ms
11,984 KB
testcase_12 AC 23 ms
9,776 KB
testcase_13 AC 26 ms
11,892 KB
testcase_14 AC 21 ms
6,544 KB
testcase_15 AC 64 ms
28,212 KB
testcase_16 AC 57 ms
24,380 KB
testcase_17 AC 131 ms
53,040 KB
testcase_18 AC 50 ms
22,184 KB
testcase_19 AC 163 ms
54,844 KB
testcase_20 AC 210 ms
54,896 KB
testcase_21 AC 119 ms
44,664 KB
testcase_22 AC 19 ms
5,812 KB
testcase_23 AC 19 ms
5,816 KB
testcase_24 AC 158 ms
54,928 KB
testcase_25 AC 74 ms
32,516 KB
testcase_26 AC 119 ms
46,708 KB
testcase_27 AC 64 ms
28,340 KB
testcase_28 AC 50 ms
22,148 KB
testcase_29 AC 113 ms
42,748 KB
testcase_30 AC 120 ms
42,608 KB
testcase_31 AC 116 ms
46,648 KB
testcase_32 AC 44 ms
17,972 KB
testcase_33 AC 44 ms
18,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef int _loop_int;
#define REP(i,n) for(_loop_int i=0;i<(_loop_int)(n);++i)
#define FOR(i,a,b) for(_loop_int i=(_loop_int)(a);i<(_loop_int)(b);++i)
#define FORR(i,a,b) for(_loop_int i=(_loop_int)(b)-1;i>=(_loop_int)(a);--i)

#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()

#define CHMIN(a,b) a=min((a),(b))
#define CHMAX(a,b) a=max((a),(b))

// mod
const ll MOD = 1000000007ll;
#define FIX(a) ((a)%MOD+MOD)%MOD

int n;
ll l,r;
ll fact[125252];
ll ifact[125252];
ll modinv(ll a){
  ll r=1;
  ll b=MOD-2;
  while(b){
    if(b&1)r=r*a%MOD;
    a=a*a%MOD;
    b>>=1;
  }
  return r;
}

ll dp[125252][61];

int main(){
  cin>>n>>l>>r;
  
  if(false){
    map<vi,int> cnt;
    map<vi,vector<vi>> lst;
    vi v(n);
    REP(i,n)v[i] = i+1;
    do{
      vi x = v;
      REP(i,n-1){
        if(x[i] > x[i+1])swap(x[i],x[i+1]);
      }
      cnt[x]++;
      lst[x].push_back(v);
    }while(next_permutation(ALL(v)));
    int ans = 0;
    for(auto P : cnt){
      vi V = P.first;
      int cnt = P.second;
      printf("%d\n",cnt);
      if(l<=cnt && cnt<=r){
        ans++;
        DEBUG_VEC(V);
      }
    }
    printf("%d\n",ans);
  }

  fact[0] = 1;
  FOR(i,1,125252)fact[i]=fact[i-1]*i%MOD;
  REP(i,125252)ifact[i]=modinv(fact[i]);
  
  // dp
  dp[0][0] = 1;
  FOR(k,1,60){
    ll sum = 0;
    FOR(l,1,n+1){
      sum += dp[l-1][k-1] * ifact[l-1] % MOD;
      sum %= MOD;
      dp[l][k] = fact[l-1] * sum % MOD;
    }
  }
  // calc
  ll ans = 0;
  // cnt == 0
  if(l==0){
    ans = fact[n-1] * (n-1) % MOD;
  }
  // cnt >= 2
  int x = 1;
  while((1ll<<x) <= r){
    if((1ll<<x) < l){
      x++;continue;
    }
    ans += dp[n-1][x] % MOD;
    ans %= MOD;
    x++;
  }
  printf("%lld\n",ans);
  return 0;
}
0