因子和

题目描述

输入两个正整数a和b,求ab的因子和。结果太大,只要输出它对9901的余数。

输入输出格式

输入格式:

仅一行,为两个正整数a和b(0≤a,b≤50000000)。

输出格式:

a^b的因子和对9901的余数。

输入输出样例

输入样例#1

2 3

输出样例#1

15

题解

根据约数和定理: 若$$n = \prod_{i = 1}^k = p_1^{a_1}\times p_2^{a_2} \times p_3^{a_3} \times \cdots \times p_k^{a_k}(其中p_i为n的质因数,a_i为正整数)$$
\(n\)的所有正约数之和为$$f(n) = \prod_{i = 1}^k(\sum_{j = 0}{a_i}pj) \=(1 + p_1 + p_1 ^ 2 + p_1^3+\cdots+ p_1^{a1}) \times(1 + p_2 + p_22+p_23+\cdots+p_2^{a_2})\times \dots \times (1 + p_k + p_k2+p_k3+\cdots + p_k^{a_k}$$

\(S = 1 + p_k + p_k^2+p_k^3+\cdots + p_k^{a_k}\)\(p_kS = p_k + p_k^2+p_k^3+\cdots + p_k^{a_k}+p_k^{a_k+1}\)
后面的式子减前面的式子得:\((p_k - 1)S = p_k^{a_k+1} - 1\)即$$S = \frac{p_k^{a_k+1} - 1}{p_k - 1}$$
最后通过逆元+快速幂计算即可

#include<bits/stdc++.h>
#define gc getchar
#define ll long long
inline ll read(){ll x = 0; char ch = gc(); bool positive = 1;for (; !isdigit(ch); 
ch = gc()) if (ch == '-')  positive = 0;for (; isdigit(ch); ch = gc())  x = x * 10 
+ ch - '0';return positive ? x : -x;}inline void write(ll a){if(a>=10)write(a/10);
putchar('0'+a%10);}inline void writeln(ll a){if(a<0){a=-a; putchar('-');}write(a);
puts("");}

using namespace std;

const int N = 10000, p = 9901;

ll num[N], cnt[N], tot;

inline ll ksm(ll a, ll b) {
	ll res = 1;
	for(; b; b >>= 1, a = a * a % p)
		if(b & 1)
			res = res * a % p;
	return res;
}

int main() {
	ll x, a, b, y, ans = 1;
	x = read(), y = read();
	for(ll i = 2; i <= x; ++i)
		if(x % i == 0) {
			num[++tot] = i;
			while(x % i == 0) {
				++cnt[tot];
				x /= i;
			}
			cnt[tot] *= y;
		}
	for(int i = 1; i <= tot; ++i) {
		a = ksm(num[i], cnt[i] + 1) - 1;
		b = ksm(num[i] - 1, p - 2);
		ans = ans * a * b % p;
	}
	writeln((ans + p ) % p);
	return 0;
}
posted @ 2018-04-27 18:03  绍兴土匪  阅读(181)  评论(0编辑  收藏  举报