poj-1845 Sumdiv (逆元+费马小定理+因子和)

45 篇文章 0 订阅

分析:与hdu 1452很类似,就不详细解释了,详细解释

#include<iostream>
#include<cstdio>
#include<cstring>
typedef long long LL;
using namespace std ;

LL m[205][2];
int cnt;
 
void exgcd(LL a,LL b,LL &x,LL &y) 
{
	if(b==0){
		x=1;
		y=0;
		return;
	}
	exgcd(b,a%b,x,y);
	LL t=x-a/b*y;
	x=y;
	y=t;
	return;
}

void eular(LL x)
{
	cnt=0;
	for(LL i=2;i*i<=x;i++){
		if(x%i==0){
			m[cnt][0]=i;
			m[cnt][1]=1;
			x/=i;
			while(x%i==0){
				x/=i;
				m[cnt][1]++;
			}
			cnt++;
		}
	}
	if(x>1){
		m[cnt][0]=x;
		m[cnt++][1]=1;
	}
}

LL get_ans(LL x,LL y)
{
	x%=9901;
	if(x==0) return 9900;  //因为答案不能为负数,而输出的ans-1<0 ,所以借9901-1; 
	if(x==1) return  0;    //ans=1-1; 
	LL ans=1;
	y%=9900;
	while(y){
		if(y&1)ans=(ans*x)%9901;
		y>>=1;
		x=(x*x)%9901;
	}
	return ans-1;
}

int main()
{
    LL a,b,ans;
    LL x,y;
    while(scanf("%lld %lld",&a,&b)!=EOF){
		eular(a);
		ans=1;
		for(int i=0;i<cnt;i++) m[i][1]*=b;
		for(int i=0;i<cnt;i++){
			if(m[i][0]%9901==0) continue;    //它的求余值为1。
			if(m[i][0]%9901==1){               //这个很重要,因为它求逆元不存在,如果不判,答案可能会错的。
				ans=ans*(m[i][1]+1)%9901;       //这个我纠结了好久,我认为:p^k/(p-1)%(p-1)=p^(k-1)%(p-1),才会有这样的答案。
				continue;
			}
			ans=ans*get_ans(m[i][0],m[i][1]+1)%9901;
			exgcd(m[i][0]-1,9901,x,y);                //求m[i][0]-1关于9901的逆元。 
			x=(x%9901+9901)%9901;
			ans=ans*x%9901;
		}
		printf("%lld\n",ans);
    }
 	return 0 ;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值