题解 P9380【[THUPC 2023 决赛] 总投票数】

发布时间 2023-06-03 21:23:30作者: rui_er

首先,若存在 \(P_i=1\),那么 \(K=1\)

否则,设小数点后位数为 \(L\),则有 \(K\le 10^L\)。这是因为任意一个 \(L\) 位小数,都显然可以通过一个整数除以 \(10^L\) 得到。

于是枚举分母 \(k\le 10^L\),枚举每个 \(P_i\),找到与其最接近的 \(\frac{x_i}{k}\),判断精度是否符合要求,以及是否满足 \(\sum\frac{x_i}{k}=1\)\(\sum x_i=k\) 的条件即可。如果满足,则 \(k\) 即为所求的 \(K\)

时间复杂度 \(\mathcal O(2^LN)\)

//By: OIer rui_er
#include <bits/stdc++.h>
#define rep(x,y,z) for(ll x=(y);x<=(z);x++)
#define per(x,y,z) for(ll x=(y);x>=(z);x--)
#define debug(format...) fprintf(stderr, format)
#define fileIO(s) do{freopen(s".in","r",stdin);freopen(s".out","w",stdout);}while(false)
using namespace std;
typedef long long ll;

mt19937 rnd(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count());
ll randint(ll L, ll R) {
    uniform_int_distribution<ll> dist(L, R);
    return dist(rnd);
}

template<typename T> void chkmin(T& x, T y) {if(x > y) x = y;}
template<typename T> void chkmax(T& x, T y) {if(x < y) x = y;}

const ll N = 105;

ll n, L, val[N];
char s[N][10];

int main() {
    scanf("%lld", &n);
    rep(i, 1, n) {
        scanf("%s", s[i]+1);
        L = (ll)strlen(s[i]+1);
        if(s[i][1] == '1') return puts("1")&0;
        rep(j, 3, L) val[i] = 10 * val[i] + (s[i][j] ^ 48);
    }
    L -= 2; // "0."
    ll lim = 1;
    rep(i, 1, L) lim *= 10;
    rep(i, 2, lim) {
        ll sum = 0, ok = 1;
        rep(j, 1, n) {
            ll now = ll(round(1.0 * val[j] / lim * i));
            sum += now; // check sum to 1
            if(ll(round(1.0 * now / i * lim)) != val[j]) {ok = 0; break;} // check precision
        }
        if(sum == i && ok) return printf("%lld\n", i)&0;
    }
    return 0;
}