結果

問題 No.2067 ±2^k operations
ユーザー RubikunRubikun
提出日時 2022-09-02 23:18:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 12 ms / 2,000 ms
コード長 2,701 bytes
コンパイル時間 2,183 ms
コンパイル使用メモリ 202,708 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-10 05:42:15
合計ジャッジ時間 4,034 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 6 ms
4,380 KB
testcase_02 AC 6 ms
4,380 KB
testcase_03 AC 5 ms
4,380 KB
testcase_04 AC 5 ms
4,380 KB
testcase_05 AC 6 ms
4,380 KB
testcase_06 AC 5 ms
4,380 KB
testcase_07 AC 11 ms
4,380 KB
testcase_08 AC 11 ms
4,380 KB
testcase_09 AC 11 ms
4,376 KB
testcase_10 AC 11 ms
4,380 KB
testcase_11 AC 11 ms
4,384 KB
testcase_12 AC 11 ms
4,380 KB
testcase_13 AC 11 ms
4,380 KB
testcase_14 AC 11 ms
4,376 KB
testcase_15 AC 11 ms
4,384 KB
testcase_16 AC 11 ms
4,380 KB
testcase_17 AC 11 ms
4,380 KB
testcase_18 AC 12 ms
4,376 KB
testcase_19 AC 3 ms
4,380 KB
testcase_20 AC 3 ms
4,380 KB
testcase_21 AC 11 ms
4,384 KB
testcase_22 AC 11 ms
4,376 KB
testcase_23 AC 11 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=998244353,MAX=300005,INF=1<<30;

// floor_sum

// from: https://gist.github.com/yosupo06/ddd51afb727600fd95d9d8ad6c3c80c9
// (based on AtCoder STL)

constexpr long long safe_mod(long long x, long long m) {
    x %= m;
    if (x < 0) x += m;
    return x;
}

unsigned long long floor_sum_unsigned(unsigned long long n,
                                      unsigned long long m,
                                      unsigned long long a,
                                      unsigned long long b) {
    unsigned long long ans = 0;
    while (true) {
        if (a >= m) {
            ans += n * (n - 1) / 2 * (a / m);
            a %= m;
        }
        if (b >= m) {
            ans += n * (b / m);
            b %= m;
        }
        
        unsigned long long y_max = a * n + b;
        if (y_max < m) break;
        n = (unsigned long long)(y_max / m);
        b = (unsigned long long)(y_max % m);
        std::swap(m, a);
    }
    return ans;
}

long long floor_sum(long long n, long long m, long long a, long long b) {
    assert(0 <= n && n < (1LL << 32));
    assert(1 <= m && m < (1LL << 32));
    unsigned long long ans = 0;
    if (a < 0) {
        unsigned long long a2 = safe_mod(a, m);
        ans -= 1ULL * n * (n - 1) / 2 * ((a2 - a) / m);
        a = a2;
    }
    if (b < 0) {
        unsigned long long b2 = safe_mod(b, m);
        ans -= 1ULL * n * ((b2 - b) / m);
        b = b2;
    }
    return ans + floor_sum_unsigned(n, m, a, b);
}

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int QQ;cin>>QQ;
    vector<ll> A={2,4,8},L={1,2,3},R={1,2,5};
    for(int i=0;i<59;i++){
        A.push_back(A.back()*2);
        L.push_back(R.back()+1);
        if(i%2==0){
            R.push_back(R.back()*2);
        }else{
            R.push_back(R.back()*2+1);
        }
    }
    //for(int i=0;i<si(A);i++) cout<<A[i]<<" "<<L[i]<<" "<<R[i]<<endl;
    while(QQ--){
        ll N;cin>>N;
        ll ans=0;
        for(int t=0;t<si(A);t++){
            if(N<L[t]) break;
            ll rem=N-L[t];
            ans+=((rem+1)/A[t])*(R[t]-L[t]+1);
            rem++;
            rem%=A[t];
            ans+=min(rem,R[t]-L[t]+1);
            //cout<<ans<<" ";
        }
        cout<<ans<<"\n";
    }
}
0