2017 CCPC 杭州 B题 /hdu 6265 (状态压缩/规律)
程序员文章站
2022-03-30 23:32:07
...
‘
解法1代码:
//577ms
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <queue>
#include <stack>
#include <math.h>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 1e5 + 10;
const ll mod = 998244353;
ll p[22], q[22], inv[22];
ll sum, ans;
int n;
ll q_pow(ll a, ll b)
{
ll res = 1;
while (b) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
b /= 2;
}
return res;
}
void dfs(int pos, ll cnt)
{
if (pos == n) {
ans = (ans + cnt) % mod;
return;
}
dfs(pos + 1, cnt);
dfs(pos + 1, cnt * q[pos] % mod * (p[pos] - 1) % mod * inv[pos] % mod);
}
int main(void)
{
#ifdef ACM
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif
int t;
scanf("%d", &t);
while (t--) {
sum = 1;
ans = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%lld%lld", p + i, q + i);
inv[i] = q_pow(p[i], mod - 2);
sum = sum * q_pow(p[i], q[i]) % mod;
}
dfs(0, sum);
printf("%lld\n", ans);
}
return 0;
}
解法二代码:
//15ms
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <queue>
#include <stack>
#include <math.h>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 1e5 + 10;
const ll mod = 998244353;
ll p[22], q[22], inv[22];
ll sum;
ll q_pow(ll a, ll b)
{
ll res = 1;
while (b) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
b /= 2;
}
return res;
}
int main(void)
{
#ifdef ACM
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif
int t, m;
scanf("%d", &t);
while (t--) {
sum = 1;
scanf("%d", &m);
for (int i = 0; i < m; i++) {
scanf("%lld%lld", p + i, q + i);
inv[i] = q_pow(p[i], mod - 2);
sum = sum * q_pow(p[i], q[i]) % mod;
}
for (int i = 0; i < m; i++) {
sum = sum * (((q[i] * (p[i] - 1) % mod) * inv[i] + 1) % mod) % mod;
}
printf("%lld\n", sum % mod);
}
return 0;
}
上一篇: 这饭量我都打不过你