結果

問題 No.1289 RNG and OR
ユーザー fastmathfastmath
提出日時 2020-11-13 22:34:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 119 ms / 2,000 ms
コード長 3,388 bytes
コンパイル時間 1,928 ms
コンパイル使用メモリ 202,568 KB
実行使用メモリ 9,292 KB
最終ジャッジ日時 2023-09-30 03:34:36
合計ジャッジ時間 3,232 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
7,472 KB
testcase_01 AC 4 ms
7,460 KB
testcase_02 AC 3 ms
7,448 KB
testcase_03 AC 3 ms
7,512 KB
testcase_04 AC 4 ms
7,588 KB
testcase_05 AC 4 ms
7,468 KB
testcase_06 AC 3 ms
7,460 KB
testcase_07 AC 3 ms
7,464 KB
testcase_08 AC 3 ms
7,476 KB
testcase_09 AC 4 ms
7,460 KB
testcase_10 AC 4 ms
7,484 KB
testcase_11 AC 4 ms
7,512 KB
testcase_12 AC 4 ms
7,524 KB
testcase_13 AC 4 ms
7,692 KB
testcase_14 AC 5 ms
7,488 KB
testcase_15 AC 7 ms
7,508 KB
testcase_16 AC 10 ms
7,528 KB
testcase_17 AC 17 ms
7,508 KB
testcase_18 AC 32 ms
7,648 KB
testcase_19 AC 61 ms
8,232 KB
testcase_20 AC 119 ms
9,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
#define debug(x) std::cout << #x << ": " << x << '\n';

const int MOD = 998'244'353;
//need define int long long
int mod(int n) {
    n %= MOD;
    if (n < 0) return n + MOD;
    else return n;
}   
int fp(int a, int p) {
    int ans = 1, c = a;
    for (int i = 0; (1ll << i) <= p; ++i) {
        if ((p >> i) & 1) ans = mod(ans * c);
        c = mod(c * c);
    }   
    return ans;
}   
int dv(int a, int b) { return mod(a * fp(b, MOD - 2)); }

struct M {
ll x;
M (int x_) {
    x = mod(x_);
}   
M () {
    x = 0;
}
M operator + (M y) {
    int ans = x + y.x;
    if (ans >= MOD)
        ans -= MOD;
    return M(ans);
}
M operator - (M y) {
    int ans = x - y.x;
    if (ans < 0)
        ans += MOD;
    return M(ans);            
}   
M operator * (M y) {
    return M(x * y.x % MOD);   
}   
M operator / (M y) {
    return M(x * fp(y.x, MOD - 2) % MOD);
}   
M operator + (int y) {
    return (*this) + M(y);
}
M operator - (int y) {
    return (*this) - M(y);
}   
M operator * (int y) {
    return (*this) * M(y);
}   
M operator / (int y) {
    return (*this) / M(y);
}   
M operator ^ (int p) {
    return M(fp(x, p));
}   
void operator += (M y) {
    *this = *this + y;
}   
void operator -= (M y) {
    *this = *this - y;
}   
void operator *= (M y) {
    *this = *this * y;
}
void operator /= (M y) {
    *this = *this / y;
}   
void operator += (int y) {
    *this = *this + y;
}   
void operator -= (int y) {
    *this = *this - y;
}   
void operator *= (int y) {
    *this = *this * y;
}
void operator /= (int y) {
    *this = *this / y;
}   
void operator ^= (int p) {
    *this = *this ^ p;
}
};  
const int N = 18;
M p[1 << N];
M sub[1 << N];

signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #else
    #define endl '\n'
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cout.setf(ios::fixed); cout.precision(20); 
    #endif

    int n;
    cin >> n;
    vector <int> a(1 << n); 
    int s = 0;
    for (int i = 0; i < (1 << n); ++i) {
        cin >> a[i];
        s += a[i];
    }   
    for (int i = 0; i < (1 << n); ++i) {
        p[i] = M(a[i])/s;
        sub[i] = p[i];
    }   

    for (int b = 0; b < n; ++b)
        for (int i = 0; i < (1 << n); ++i)
            if ((i>>b) & 1) 
                sub[i] += sub[i - (1<<b)];

    auto f = [&] (int mask) {
        int oth = (1 << n) - 1 - mask;
        return sub[oth];
    };  

    auto sum = [&] (M a) {
        assert(a.x != 1);
        return a / (M(1) - a);
    };  

    /*
    for (int i = 1; i <= 100; ++i) {
        for (int j = 1; j <= 100; ++j) {
            M t = M(i)/M(j);
            if (t.x == 333333338) {
                cout << i << ' ' << j << endl;
                exit(0);
            }   
        }   
    }
    */  

    M ans(0);
    for (int mask = 1; mask < (1 << n); ++mask) {
        //cout << mask << ' ' << f(mask).x << ' ' << sum(f(mask)).x << endl;
        
        if (bp(mask)&1) {
            ans += sum(f(mask));
        }   
        else {
            ans -= sum(f(mask)); 
        }   
    }   
    cout << (ans+1).x << endl;
}
0