(10.16 训练)codeforces C. Primes and Multiplication

题目:http://codeforces.com/contest/1228/problem/C

题意:就不细述了。

题解:对于一个10^9以内的数分解质因数(数量较少),计算每一个质因数在1~n(10^18)每个数的贡献值。

假如有一质因数3,n为28

  • 28/3 1 2 1x3 4 5 2x3 7 8 3x3 10 11 4x3 13 14 5x3 16 17 6x3 19 20 7x3 22 23 8x3 25 26 9x3 28
  • 9/3   1 2 1x3 4 5 2x3 7 8 3x3
  • 3/3   1 2 1x3

计算贡献值   3^(9+3+1)

关键代码:

类似于求 m!下质因数n的个数

参考:https://blog.csdn.net/running_in_dark/article/details/53453659

LL solve(LL n,LL p)
{
	LL res=0;
	while(n)
	{
		res+=n/p;
		n/=p;
	}
	return pow(p,res);
}

代码:

#include<bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
const int maxn=1005;
const int mod=1e9+7;
LL x,n;
vector<int>pm;
void cal(int n)
{
	for(int i=2;i<=sqrt(n);i++)
	{
		if(n%i==0)
		{
			pm.push_back(i);
			while(n%i==0)
			{
				n/=i;
			}
		}
	}
	if(n>1)
		pm.push_back(n);
}
LL pow(LL a,LL n)
{
	LL res=1;
	while(n)
	{
		if(n&1)
			res*=a,res%=mod;
		n>>=1;
		a*=a;
		a%=mod;
	}
	return res;
}
LL solve(LL n,LL p)
{
	LL res=0;
	while(n)
	{
		res+=n/p;
		n/=p;
	}
	return pow(p,res);
}
int main()
{
	cin>>x>>n;
	cal(x);
	LL ans=1;
	for(int i=0;i<pm.size();i++)
	{
		ans=ans*solve(n,pm[i]);
		ans%=mod;
	}
	cout << ans << endl;
	return 0;
} 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值