Light OJ 1054 Efficient Pseudo Code 求n^m的约数和

题目来源:Light OJ 1054 Efficient Pseudo Code

题意:求n的m次这个数的所有的约数和

思路:首先对于一个数n = p1^a1*p2^a2*p3^a3*…*pk^ak  约束和s = (p1^0+p1^1+p1^2+…p1^a1)(p2^0+p2^1+p2^2+…p2^a2)…(pk^0+pk^1+pk^2+…pk^ak)

然后就是先求素数表 分解因子 然后求p1^0+p1^1+p1^2+…p1^a1 这是一个以1开始的等比数列 就是(1-q^n)/(1-q) 最高次用快速幂求 此外有除法 除以(1-q)乘以(1-q)的逆元

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long LL;
const long long mod = 1000000007;
const int maxn = 1000010;
//筛素数
int vis[maxn];
LL prime[maxn];

LL pow_mod(LL a, LL p)
{
	LL ans = 1;
	while(p)
	{
		if(p&1)
		{
			ans *= a;
			ans %= mod;
		}
		a *= a;
		a %= mod;
		p >>= 1;
	}
	return ans;
}

void sieve(int n)
{
	int m = sqrt(n+0.5);
	memset(vis, 0, sizeof(vis));
	vis[0] = vis[1] = 1;
	for(int i = 2; i <= m; i++)
		if(!vis[i])
			for(int j = i*i; j <= n; j += i)
				vis[j] = 1;
}

int get_primes(int n)
{
	sieve(n);
	int c = 0;
	for(int i = 2; i <= n; i++)
		if(!vis[i])
			prime[c++] = i;
	return c;
}
int main()
{
	int c = get_primes(100000);
	int cas = 1;
	int T;
	scanf("%d", &T);
	while(T--)
	{
		LL n, m, ans = 1;
		scanf("%lld %lld", &n, &m);

		for(int i = 0; i < c && prime[i]*prime[i] <= n; i++)
		{
			if(n%prime[i] == 0)
			{
				int sum = 0;
				while(n%prime[i] == 0)
				{
					sum++;
					n /= prime[i];

				}
				ans *= pow_mod(prime[i], sum*m+1)-1;
				ans %= mod;
				ans *= pow_mod(prime[i]-1, mod-2);
				ans %= mod;
				//ans = (ans+mod)%mod;
			}
		}
		if(n > 1)
		{
			ans *= pow_mod(n%mod, m+1)-1;
			ans %= mod;
			ans *= pow_mod((n-1)%mod, mod-2);
			ans %= mod;
			ans = (ans+mod)%mod;//此句不加会错 原因不明 求告知
		}
		printf("Case %d: %lld\n", cas++, ans);
	}
	return 0;
}

Light OJ 1054 Efficient Pseudo Code 求n^m的约数和

时间: 2024-08-13 04:38:35

Light OJ 1054 Efficient Pseudo Code 求n^m的约数和的相关文章

1054 - Efficient Pseudo Code

   PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 32 MB Sometimes it's quite useful to write pseudo codes for problems. Actually you can write the necessary steps to solve a particular problem. In this problem you are given a ps

Lightoj 1054 - Efficient Pseudo Code

题目连接: http://www.lightoj.com/volume_showproblem.php?problem=1054 题目大意: 给出n,m,问n^m的所有因子之和是多少? 解题思路: 补充知识: 1:对于一个数字n=p1^t1+p2^t2+p3^t3+.........+pn^tn.求n因子和等价于n所有因子的子集所对应值相加之和—(p1^0+p1^1+p1^2+......+p1^t1)*(p2^0+p2^1+......+p2^t2)*.....*(pn^0+pn^1+....

Light OJ 1028 Trailing Zeroes (I) 求n因子数

今天真机调试的时候莫名其妙遇到了这样的一个问题: This product type must be built using a provisioning profile, however no provisioning profile matching both the identity "iPhone Developer" and the bundle identifier..... 具体如下图所示: 十分蛋疼, 发现不管是从网上下的demo, 还是自己的过程.凡事真机测试的时候都

light oj 1045 - Digits of Factorial(求阶乘在不同进制下的位数)

Factorial of an integer is defined by the following function f(0) = 1 f(n) = f(n - 1) * n, if(n > 0) So, factorial of 5 is 120. But in different bases, the factorial may be different. For example, factorial of 5 in base 8 is 170. In this problem, you

Light OJ 1411 Rip Van Winkle`s Code 线段树成段更新

题目来源:Light OJ 1411 Rip Van Winkle`s Code 题意:3中操作 1种查询 求区间和 其中每次可以把一段区间从左到右加上1,2,3,...或者从右到左加上...3,2,1 或者把某个区间的数都置为v 思路:我是加了6个域 add是这段区间每个数都要加上add  add是这么来的 对与123456...这个等差数列 可能要分为2个区间 那么我就分成123和123 两个右边的等差数列每个数还应该加上3 所以右区间add加3 v是这个区间都要置为v 他的优先级最高 b是

Light OJ 1026 Critical Links 求桥

题目 Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,3], a solution is: [ [3], [1], [2], [1,2,3]

Light OJ 1288 Subsets Forming Perfect Squares 高斯消元求矩阵的秩

题目来源:Light OJ 1288 Subsets Forming Perfect Squares 题意:给你n个数 选出一些数 他们的乘积是完全平方数 求有多少种方案 思路:每个数分解因子 每隔数可以选也可以不选 0 1表示 然后设有m种素数因子 选出的数组成的各个因子的数量必须是偶数 组成一个m行和n列的矩阵 每一行代表每一种因子的系数 解出自由元的数量 #include <cstdio> #include <cstring> #include <algorithm&g

light oj 1348 树链剖分(单点更新区间求值)

http://lightoj.com/volume_showproblem.php?problem=1348 Finally the Great Magical Lamp was in Aladdin's hand. Now he wanted to return home. But he didn't want to take any help from the Genie because he thought that it might be another adventure for hi

Light OJ 1341 Aladdin and the Flying Carpet

It's said that Aladdin had to solve seven mysteries before getting the Magical Lamp which summons a powerful Genie. Here we are concerned about the first mystery. Aladdin was about to enter to a magical cave, led by the evil sorcerer who disguised hi