2017 CCPC 杭州 B題 /hdu 6265 (狀態壓縮/規律)

解法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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章