結果

問題 No.1929 Exponential Sequence
ユーザー AngrySadEightAngrySadEight
提出日時 2022-05-06 22:33:10
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 117 ms / 2,000 ms
コード長 3,218 bytes
コンパイル時間 1,767 ms
コンパイル使用メモリ 174,348 KB
実行使用メモリ 24,964 KB
最終ジャッジ日時 2023-10-12 04:22:38
合計ジャッジ時間 3,628 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 111 ms
23,304 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 2 ms
4,352 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 1 ms
4,352 KB
testcase_13 AC 1 ms
4,352 KB
testcase_14 AC 1 ms
4,348 KB
testcase_15 AC 2 ms
4,352 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 1 ms
4,352 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 113 ms
24,448 KB
testcase_22 AC 73 ms
14,344 KB
testcase_23 AC 111 ms
23,972 KB
testcase_24 AC 50 ms
13,628 KB
testcase_25 AC 112 ms
22,508 KB
testcase_26 AC 117 ms
24,964 KB
testcase_27 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = (int)(n); i >= 0; i--)
#define all(v) v.begin(), v.end()
#define mod1 1000000007
#define mod2 998244353
typedef long long ll;

ll my_pow(ll x, ll n, ll mod){
    // 繰り返し二乗法.x^nをmodで割った余り.
    ll ret;
    if (n == 0){
        ret = 1;
    }
    else if (n % 2 == 1){
        ret = (x * my_pow((x * x) % mod, n / 2, mod)) % mod;
    }
    else{
        ret = my_pow((x * x) % mod, n / 2, mod);
    }
    return ret;
}

int main(){
    ll n, S;
    cin >> n >> S;
    vector<ll> a(n);
    rep(i,n) cin >> a[i];
    
    if (n <= 4){
        vector<vector<ll>> vec(n + 1, vector<ll>(0));
        vec[0].push_back(0);
        for (ll i = 0; i < n; i++){
            ll now = a[i];
            while(true){
                if (now > S){
                    break;
                }
                for (ll j = 0; j < vec[i].size(); j++){
                    if (now + vec[i][j] <= S){
                        vec[i + 1].push_back(now + vec[i][j]);
                    }
                }
                now = now * a[i];
            }
        }
        /*for (ll i = 0; i < vec[n].size(); i++){
            cout << vec[n][i] << " ";
        }
        cout << endl;*/
        cout << vec[n].size() << endl;
    }
    else{
        vector<vector<ll>> former(5, vector<ll>(0));
        vector<vector<ll>> latter(n - 3, vector<ll>(0));
        former[0].push_back(0);
        latter[0].push_back(0);
        for (ll i = 0; i < 4; i++){
            ll now = a[i];
            while(true){
                if (now > S){
                    break;
                }
                for (ll j = 0; j < former[i].size(); j++){
                    if (now + former[i][j] <= S){
                        former[i + 1].push_back(now + former[i][j]);
                    }
                }
                now = now * a[i];
            }
        }
        for (ll i = 4; i < n; i++){
            ll now = a[i];
            while(true){
                if (now > S){
                    break;
                }
                for (ll j = 0; j < latter[i - 4].size(); j++){
                    if (now + latter[i - 4][j] <= S){
                        latter[i - 3].push_back(now + latter[i - 4][j]);
                    }
                }
                now = now * a[i];
            }
        }
        sort(all(former[4]));
        sort(all(latter[n - 4]));
        if (latter[n - 4].size() == 0){
            cout << 0 << endl;
        }
        else{
            ll ans = 0;
            for (ll i = 0; i < former[4].size(); i++){
                if (latter[n - 4][0] + former[4][i] > S){
                    ans += 0;
                }
                else if (latter[n - 4][latter[n - 4].size() - 1] + former[4][i] <= S){
                    ans += (latter[n - 4].size());
                }
                else{
                    auto itr = upper_bound(all(latter[n - 4]), S - former[4][i]);
                    ll X = itr - latter[n - 4].begin();
                    ans += X;
                }
            }
            cout << ans << endl;
        }
    }
}
0