結果

問題 No.603 hel__world (2)
ユーザー rickythetarickytheta
提出日時 2017-12-24 12:24:08
言語 C++11
(gcc 11.4.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 4,124 bytes
コンパイル時間 1,893 ms
コンパイル使用メモリ 173,856 KB
実行使用メモリ 53,716 KB
最終ジャッジ日時 2024-12-17 18:31:34
合計ジャッジ時間 20,818 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
18,140 KB
testcase_01 AC 13 ms
18,136 KB
testcase_02 AC 12 ms
18,064 KB
testcase_03 AC 11 ms
26,712 KB
testcase_04 AC 12 ms
18,160 KB
testcase_05 AC 12 ms
18,228 KB
testcase_06 AC 12 ms
18,120 KB
testcase_07 AC 12 ms
11,188 KB
testcase_08 AC 12 ms
11,244 KB
testcase_09 AC 12 ms
11,120 KB
testcase_10 AC 12 ms
11,104 KB
testcase_11 AC 414 ms
15,308 KB
testcase_12 AC 209 ms
15,268 KB
testcase_13 AC 434 ms
15,308 KB
testcase_14 AC 12 ms
11,256 KB
testcase_15 AC 12 ms
11,228 KB
testcase_16 AC 12 ms
11,280 KB
testcase_17 AC 12 ms
11,276 KB
testcase_18 AC 12 ms
11,204 KB
testcase_19 AC 12 ms
11,192 KB
testcase_20 AC 60 ms
14,100 KB
testcase_21 AC 60 ms
14,100 KB
testcase_22 AC 12 ms
11,280 KB
testcase_23 AC 12 ms
11,232 KB
testcase_24 AC 12 ms
11,340 KB
testcase_25 TLE -
testcase_26 TLE -
testcase_27 TLE -
testcase_28 TLE -
testcase_29 AC 59 ms
14,096 KB
testcase_30 AC 38 ms
12,596 KB
testcase_31 AC 11 ms
11,240 KB
testcase_32 AC 64 ms
53,716 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
#define MOD 1000003
#define FIX(a) ((a)%MOD+MOD)%MOD

typedef long double Real;
// typedef __float128 Real;

ll mita[MOD+1];

ll comb(ll n, ll k){
  if(k>n)return 0;
  if(k==n || k==0)return 1;
  ll ret = 1;
  REP(i,k)ret=ret*((n-i)%MOD)%MOD;
  FOR(i,1,k+1)ret=ret*mita[i]%MOD;
  return ret;
}

/*
sum_i floor(((t[i]-1)*x+t[i]) / x)+1 <= s

floor(y) >= y-1
{sum_i ((t[i]-1)*x+t[i]) / x} <= s
sum_i (t[i]-1)*x + t[i] <= s * x
((sum_i t[i])-n)*x + (sum_i t[i]) <= s * x
(sum_i t[i])*(1+x) <= (s+n) * x
sum_i t[i] <= (s + n - sum_i t[i]) * x
x >= (sum_i t[i]) / (s + n - sum_i t[i])

sum_i floor(((t[i]-1)*x+t[i]) / x)+1 <= s
sum_i floor(((t[i]-1)*x+t[i]) / x) <= s-n
sum_i floor(((t[i]-1)*x+t[i]) / x) + n >= s-n
floor(y) <= y+1
sum_i ((t[i]-1)*x+t[i]) / x + 2n >= s-n
sum_i (1+x)*t[i] - nx >= (s-3n)*x
sum_i t[i] >= (s - 2n - sum_i t[i]) * x
x <= (sum_i t[i]) / (s - 2n - sum_i t[i])
*/

struct frac{
  ll u, d;
  frac(ll u,ll d):u(u),d(d){
    ll g = __gcd(u,d);
    u /= g;
    d /= g;
  }
  bool operator<(const frac &that) const{
    ll au = u, ad = d;
    ll nu = that.u, nd = that.d;
    if(au==nu && ad==nd)return false;
    while(true){
      if(ad==0)return false;
      if(nd==0)return true;
      ll av = au/ad;
      ll nv = nu/nd;
      if(av != nv){
        return av < nv;
      }
      ll v = au / ad;
      au -= v*ad;
      nu -= v*nd;
      swap(au, ad);
      swap(nu, nd);
      swap(au, nu);
      swap(ad, nd);
    }
  }
};

ll solve(ll n, ll s, vl t){
  if(n==0)return 1ll;
  ll tsum = accumulate(ALL(t),0ll);
  if(tsum > s)return 0ll;
  if(tsum == s)return 1ll;
  // binary search
  Real low = tsum / ((Real)s + (Real)n - tsum);
  Real high = tsum / ((Real)s - (Real)(n*2) - tsum);
  // Real low = 0.0, high = 1048575.0;
  REP(_,50){
    Real th = (low+high)/2.0;
    ll xsum = 0;
    bool over = false;
    REP(i,n){
      // Real ri = (1.0 - th + t[i]*th) / (th-1.0);
      Real ri = ((t[i]-1.0)*th+t[i]) / th;
      if(ri + xsum >= (Real)s){
        over = true;
        break;
      }
      ll x = (ll)floorl(ri)+1ll;
      CHMAX(x, t[i]);
      xsum += x;
    }
    if(!over && xsum <= s){
      high = th;
    }else{
      low = th;
    }
  }
  Real th = high;
  // rest
  ll xsum = 0;
  // priority_queue<pair<Real,int>> Q;
  priority_queue<pair<frac,int>> Q;
  vl xs(n);
  REP(i,n){
    // Real ri = (1.0 - th + t[i]*th) / (th-1.0);
    Real ri = ((t[i]-1.0)*th+t[i]) / th;
    ll x = (ll)floorl(ri)+1ll;
    CHMAX(x, t[i]);
    xsum += x;
    xs[i] = x;
    // Q.push(make_pair((Real)(x+1)/(x+1-t[i]),i));
    Q.push(make_pair(frac(x+1,x+1-t[i]), i));
  }
  while(xsum < s){
    auto P = Q.top(); Q.pop();
    int id = P.second;
    xs[id]++;
    ll x = xs[id];
    // Q.push(make_pair((Real)(x+1)/(x+1-t[id]),id));
    Q.push(make_pair(frac(x+1,x+1-t[id]), id));
    xsum++;
  }
  // calc ans
  ll ans = 1ll;
  REP(i,n){
    ans = ans * comb(xs[i], t[i]) % MOD;
  }
  return ans;
}

int main(){
  // mod inverse table
  mita[0] = 0;
  mita[1] = 1;
  FOR(i,2,MOD+1){
    mita[i] = MOD - (ll)(MOD/i) * mita[MOD%i] % MOD;
  }
  // input
  vl s(26,0);
  REP(i,26)cin>>s[i];
  string t;
  cin>>t;
  t = "$" + t + "$";
  // calc
  ll ans = 1ll;
  REP(i,26){
    char c = 'a'+i;
    vl ts;
    int cnt = 0;
    REP(j,t.size()){
      if(t[j]!=c){
        if(cnt > 0){
          ts.push_back(cnt);
        }
        cnt = 0;
      }else{
        cnt++;
      }
    }
    ll x = solve(ts.size(), s[i], ts);
    ans = ans * x % MOD;
  }
  cout<<ans<<endl;
  return 0;
}
0