2017 CCPC 杭州 B题 /hdu 6265 (状态压缩/规律)

2017 CCPC 杭州 B题 /hdu 6265 (状态压缩/规律)_第1张图片

解法1代码:

//577ms
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
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;
}


你可能感兴趣的:(数论)