結果

問題 No.1929 Exponential Sequence
ユーザー null
提出日時 2025-02-02 01:46:09
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 102 ms / 2,000 ms
コード長 2,771 bytes
コンパイル時間 1,799 ms
コンパイル使用メモリ 118,112 KB
実行使用メモリ 20,964 KB
最終ジャッジ日時 2025-02-02 01:46:13
合計ジャッジ時間 3,997 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
using namespace std;
typedef long long ll;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
ll S;
cin >> n >> S;
vector<ll> a(n);
for(int i = 0; i < n; i++){
cin >> a[i];
}
// For each index, find the maximum exponent k (starting at 1) such that a[i]^k <= S.
// For each index i, possible exponents are 1,2,...,maxExp[i].
vector<int> maxExp(n, 0);
for (int i = 0; i < n; i++){
ll power = 1;
int cnt = 0;
// We must take at least k=1.
while (true) {
// Check for overflow: if power > S/a[i], then multiplying by a[i] would exceed S.
if (power > S / a[i]) break;
power *= a[i];
cnt++;
}
if(cnt < 1){
// If even a[i]^1 > S, no valid sequence exists.
cout << 0 << "\n";
return 0;
}
maxExp[i] = cnt;
}
// Split indices into two groups.
int mid = n / 2;
vector<int> groupAIndices, groupBIndices;
for (int i = 0; i < mid; i++){
groupAIndices.push_back(i);
}
for (int i = mid; i < n; i++){
groupBIndices.push_back(i);
}
// For a given group, recursively enumerate all possible sums.
auto recGroup = [&](auto &recGroup, int pos, ll sum, const vector<int>& indices, vector<ll>& store) -> void {
if (pos == indices.size()){
store.push_back(sum);
return;
}
int i = indices[pos];
ll power = 1;
// Try exponents from 1 up to maxExp[i] (if the sum does not exceed S).
for (int exp = 1; exp <= maxExp[i]; exp++){
if(exp == 1)
power = a[i]; // a[i]^1
else
power *= a[i]; // a[i]^exp, computed iteratively.
if(sum + power > S) break; // further exponents only increase the sum
recGroup(recGroup, pos + 1, sum + power, indices, store);
}
};
vector<ll> groupASums, groupBSums;
recGroup(recGroup, 0, 0LL, groupAIndices, groupASums);
recGroup(recGroup, 0, 0LL, groupBIndices, groupBSums);
// Sort one of the groups (here groupBSums) for binary search.
sort(groupBSums.begin(), groupBSums.end());
// For each sum from group A, count how many sums from group B satisfy: sum_A + sum_B <= S.
ll answer = 0;
for (ll s : groupASums){
ll remain = S - s;
// upper_bound finds the first element greater than remain.
auto it = upper_bound(groupBSums.begin(), groupBSums.end(), remain);
answer += (it - groupBSums.begin());
}
cout << answer << "\n";
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0