BZOJ4802 欧拉函数 数论

原文链接http://www.cnblogs.com/zhouzhendong/p/8117744.html


题目传送门 - BZOJ4802


题意概括

Description

已知N,求phi(N)

Input

正整数N。N<=10^18

题解

  Miller_Rabin+Pollard_Rho

  至于Pollard_Rho,我感到很奇怪。判定的时候为何不能丢第一个值!!

  请看下面两个代码,第一个对的,第二个错的……


代码

#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std;
typedef long long LL;
LL gcd(LL a,LL b){
	return b?gcd(b,a%b):a;
}
LL Mul(LL a,LL b,LL mod){
	LL ans=0;
	a%=mod;
	while (b){
		if (b&1)
			ans=(ans+a)%mod;
		b>>=1,a=(a<<1)%mod;
	}
	return ans;
}
LL Pow(LL a,LL b,LL mod){
	LL ans=1;
	a%=mod;
	while (b){
		if (b&1)
			ans=Mul(ans,a,mod);
		b>>=1,a=Mul(a,a,mod);
	}
	return ans;
}
bool Miller_Rabin(LL n){
	if (n==2)
		return 1;
	if (n<2||n%2==0)
		return 0;
	LL m=n-1,k=0;
	while (!(m&1))
		m>>=1,k++;
	for (int i=0;i<10;i++){
		LL a=rand()%(n-1)+1,x=Pow(a,m,n),y;
		for (int j=0;j<k;j++){
			y=Mul(x,x,n);
			if (y==1&&x!=1&&x!=n-1)
				return 0;
			x=y;
		}
		if (x!=1)
			return 0;
	}
	return 1;
}
LL rnd(LL x,LL n,LL c){
	return (Mul(x,x,n)+c)%n;
}
LL Pollard_Rho(LL n,LL c){
	LL x,y;
	while (1){
		x=rand()%(n-1)+1,y=rnd(x,n,c);
		while (1){
			if (x==y)
				break;
			LL d=gcd(llabs(y-x)%n,n);
			if (1<d&&d<n)
				return d;
			x=rnd(x,n,c);
			y=rnd(rnd(y,n,c),n,c);
		}
		c=rand()%n;
	}
}
LL n,x[66],pcnt;
void find(LL n){
	if (n==1)
		return;
	if (Miller_Rabin(n)){
		x[++pcnt]=n;
		return;
	}
	LL p=Pollard_Rho(n,rand()%n);
	find(p);
	find(n/p);
}
int main(){
	srand(19260817);
	scanf("%lld",&n);
	pcnt=0;
	find(n);
	sort(x+1,x+pcnt+1);
	LL yz=pcnt?x[1]-1:1;
	for (int i=2;i<=pcnt;i++)
		yz=yz*(x[i]-(bool)(x[i]!=x[i-1]));
	printf("%lld\n",yz);
	return 0;
}

  

转载于:https://www.cnblogs.com/zhouzhendong/p/BZOJ4802.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
BZOJ 2908 题目是一个数据下载任务。这个任务要下载指定的数据文件,并统计文件中小于等于给定整数的数字个数。 为了完成这个任务,首先需要选择一个合适的网址来下载文件。我们可以使用一个网络爬虫库,如Python中的Requests库,来帮助我们完成文件下载的操作。 首先,我们需要使用Requests库中的get()方法来访问目标网址,并将目标文件下载到我们的本地计算机中。可以使用以下代码实现文件下载: ```python import requests url = '目标文件的网址' response = requests.get(url) with open('本地保存文件的路径', 'wb') as file: file.write(response.content) ``` 下载完成后,我们可以使用Python内置的open()函数打开已下载的文件,并按行读取文件内容。可以使用以下代码实现文件内容读取: ```python count = 0 with open('本地保存文件的路径', 'r') as file: for line in file: # 在这里实现对每一行数据的判断 # 如果小于等于给定整数,count 加 1 # 否则,不进行任何操作 ``` 在每一行的处理过程中,我们可以使用split()方法将一行数据分割成多个字符串,并使用int()函数将其转换为整数。然后,我们可以将该整数与给定整数进行比较,以判断是否小于等于给定整数。 最后,我们可以将统计结果打印出来,以满足题目的要。 综上所述,以上是关于解决 BZOJ 2908 数据下载任务的简要步骤和代码实现。 希望对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值