【SPOJ-TRANSP】Transposing is Fun【Pólya】【欧拉函数】【二进制】

题解见更高级的版本:http://blog.csdn.net/braketbn/article/details/50668414


#include <cstdio>

using namespace std;

typedef long long LL;

const int maxn = 1000001, p = 1000003;

int a, b, n, pow[maxn], phi[maxn], prime[maxn], cnt, fact[maxn], alpha[maxn], tot;
bool isnotprime[maxn];

inline int mul(int a, int b) {
	return ((LL)a * b) % p;
}

inline int iread() {
	int f = 1, x = 0; char ch = getchar();
	for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
	for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
	return f * x;
}

void getphi() {
	isnotprime[1] = 1; phi[1] = 1;
	for(int i = 2; i < maxn; i++) {
		if(!isnotprime[i]) prime[++cnt] = i, phi[i] = (i - 1) % p;
		for(int j = 1; j <= cnt && i * prime[j] < maxn; j++) {
			isnotprime[i * prime[j]] = 1;
			if(i % prime[j] == 0) {
				phi[i * prime[j]] = phi[i] * prime[j] % p;
				break;
			}
			phi[i * prime[j]] = phi[i] * phi[prime[j]] % p;
		}
	}
}

void factor(int x) {
	tot = 0;
	for(int i = 1; i <= cnt && prime[i] * prime[i] <= n; i++) if(x % prime[i] == 0) {
		++tot;
		fact[tot] = prime[i];
		for(alpha[tot] = 0; x % prime[i] == 0; x /= prime[i]) alpha[tot]++;
	}
	if(x > 1) ++tot, fact[tot] = x, alpha[tot] = 1;
}

int ans, L, g;

void dfs(int x, int d) {
	if(d > tot) {
		ans = (ans + mul(pow[x * g], phi[L / x])) % p;
		return;
	}
	for(int i = 0; i <= alpha[d]; i++) {
		dfs(x, d + 1);
		x *= fact[d];
	}
}

int gcd(int a, int b) {
	for(; b; b ^= a ^= b ^= a %= b);
	return a;
}

void exgcd(int a, int b, LL &x, LL &y) {
	b ? (exgcd(b, a % b, y, x), y -= a / b * x) : (x = 1, y = 0);
}

int main() {
	getphi();
	pow[0] = 1; for(int i = 1; i < maxn; i++) pow[i] = (pow[i - 1] << 1) % p;

	int T = iread();
	while(T--) {
		a = iread(); b = iread();
		if(!a || !b) {
			printf("0\n");
			continue;
		}

		n = a + b;
		g = gcd(a, b);
		L = n / g; factor(L);
		ans = 0;
		dfs(1, 1);
		LL x, y;
		exgcd(L, p, x, y); x = (x % p + p) % p;
		ans = mul(ans, x);
		ans = pow[n] - ans; ans = (ans % p + p) % p;
		printf("%d\n", ans);
	}

	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值