COFO::1452D Radio Towers

Problem

Point

Design

Complexity

Code

#include<bits/stdc++.h>
#define fi first
#define se second
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rep(i, a, b) for(int i = (a); i <(b); i++)
#define r_rep(i, a, b) for(int i = (a); i >(b); i--)
typedef long long ll;
using namespace std;
const ll MOD = 998244353;

ll mul(ll x, ll y){ return (x * y) % MOD; }
ll binpow(ll x, ll y) {
    ll ret = 1;
    while(y > 0) {
        if (y % 2 == 1) ret = mul(ret, x);
        x = mul(x, x);
        y /= 2;
    }
    return ret;
}
ll divide(ll x, ll y) {
    return mul(x, binpow(y, MOD - 2));
}
void solve() {
    int n; cin >> n;
    vector<ll> dp(n + 1, 0);
    // dp[i] : 홀수의 합이 i 가 되게 하는 경우의 수
    dp[1] = 1; // 1
    dp[2] = 1; // 1 1
    dp[3] = 2; // 1 1 1, 3
    dp[4] = 3; // {1, 1, 1, 1}, {1, 3}, {3, 1}
    // ...
    for(int i = 2; i <= n; i++) dp[i] = (dp[i-1] + dp[i-2]) %MOD;
    
    ll a = dp[n], b = binpow(2, n);
    cout << divide(a, b) << '\n';
}
int main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    solve();
    return 0;
}