QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#188057 | #6887. Data Generation | neko_nyaa | AC ✓ | 86ms | 3588kb | C++14 | 1.9kb | 2023-09-25 12:46:03 | 2023-09-25 12:46:03 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const ll mod = 998244353; // faster if const
ll modpow(ll b, ll e)
{
ll ans = 1;
for (; e; b = b * b % mod, e /= 2)
if (e & 1)
ans = ans * b % mod;
return ans;
}
template <class T, int N>
struct Matrix
{
typedef Matrix M;
array<array<T, N>, N> d{};
M operator*(const M &m) const
{
M a;
rep(i, 0, N) rep(j, 0, N)
rep(k, 0, N) a.d[i][j] = (a.d[i][j] + d[i][k] * m.d[k][j]) % mod;
return a;
}
vector<T> operator*(const vector<T> &vec) const
{
vector<T> ret(N);
rep(i, 0, N) rep(j, 0, N) ret[i] = (ret[i] + d[i][j] * vec[j]) % mod;
return ret;
}
M operator^(ll p) const
{
assert(p >= 0);
M a, b(*this);
rep(i, 0, N) a.d[i][i] = 1;
while (p)
{
if (p & 1)
a = a * b;
b = b * b;
p >>= 1;
}
return a;
}
};
#define int long long
const int M = 998244353;
void solve()
{
int n, m;
cin >> n >> m;
if (m == 0)
{
cout << 0 << '\n';
return;
}
n %= M;
Matrix<int, 2> mat;
int q = modpow(n * n % M, M - 2);
mat.d[0][0] = ((n + M - 1) * (n + M - 1) + 1) % M * q % M;
mat.d[0][1] = 2 * (n + M - 1) * q % M;
mat.d[1][0] = 2 * q % M;
mat.d[1][1] = (n * n % M - 2 + M) * q % M;
mat = mat^m;
cout << (mat.d[0][1] * n) % M << '\n';
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 86ms
memory: 3588kb
input:
100000 19491001 19491001 999999999449325353 939501148 400027352 999999998707619026 999999998353720210 999999999303057191 1879045722 1874448608 999999998385974740 1710466660 109045962 999999998020190078 999999998217418921 999999998898659805 999999999999986692 999999998389218199 351693073 2130408866 1...
output:
79256423 60176292 660407710 893892599 601021657 353145264 33871948 852325473 308290667 313185571 805837881 700368815 711096361 895992243 830757079 419158396 754661682 184055154 728055491 1 323104191 72240859 553717086 361196534 971355231 232530344 149284730 833553979 48731930 0 296123988 928795526 5...
result:
ok 100000 lines