NC24664. Hypercube Random Number Generator
描述
In geometry, a hypercube is an n-dimensional analogue of a square (n = 2) and a cube (n = 3, picture below). It is a closed, compact, convex figure whose 1-skeleton consists of groups of opposite parallel line segments aligned in each of space's dimensions, perpendicular to each other and of the same length.
输入描述
The input contains two integers n, p(1 <= n <= 1e18,1 <= p <= 100), represents the dimension of the hypercube and the length of each dimension.
输出描述
Output the expected value of HRNG in one single line.
To avoid possible errors, if the answer is , you need to output , i.e., , where is the minimum number that suits .
示例1
输入:
2 100
输出:
199648919
说明:
In the sample test case 1, the expected value is 48.4, which can write as , then we get .示例2
输入:
123456789876543 21
输出:
394865429
C++11(clang++ 3.9) 解法, 执行用时: 213ms, 内存消耗: 1000K, 提交时间: 2019-04-14 18:40:25
#include <bits/stdc++.h> using namespace std; using ll = long long; const int mod = 998244353; struct Mat { int n; ll a[105][105]; Mat() { memset(a, 0, sizeof a); } void init() { memset(a, 0, sizeof a); for(int i = 1; i <= n; i++) a[i][i] = 1; } Mat operator * (const Mat &t) const { Mat res; res.n = n; for(int k = 1; k <= n; k++) for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) res.a[i][j] = (res.a[i][j] + 1ll * a[i][k] * t.a[k][j]) % mod; return res; } Mat operator ^ (ll m) const { Mat res; res.n = n; res.init(); Mat tmp(*this); while(m) { if(m & 1) res = res * tmp; tmp = tmp * tmp; m >>= 1; } return res; } }; ll p, n; ll qpow(ll x, ll n) { ll res = 1; while(n) { if(n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } int main() { cin >> n >> p; Mat f, g; f.n = g.n = p; for(int i = 1; i <= p; i++) f.a[1][i] = 1; for(int i = 1; i <= p; i++) for(int j = 1; j <= p; j++) { int x = i - 1, y = j - 1; for(int k = 1; k <= p; k++) if(k * x % p == y) { g.a[i][j]++; } } f = f * (g ^ (n - 1)); ll ans = 0; for(int i = 1; i <= p; i++) ans = (ans + 1ll * i * f.a[1][i]) % mod; ans = ans * qpow(qpow(p, n), mod - 2) % mod; cout << ans << endl; }