HDU4910 Problem about GCD
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。
本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!
题目链接:HDU4910
正解:线性筛+$Miller-Rabin$
解题报告:
我也是醉了,开始用$Pollard-rho$一直TLE,只好给成线性筛然后除,结果又一直$WA$,突然发现我的预处理只做到了$10^5$然而应该做到$10^6$…
我打了表,发现有一些神奇的规律,首先答案只能是$1$或者$n-1$。
如果$n$的质因子中有超过$2$个$2$,即是$4$的倍数则$ans=1$。
接下来的判断中,$n$是偶数,先把$n$除以二,再判断。
如果$n$有超过$1$个质因子则输出$1$,否则输出$-1$。
考虑范围内的数最多能有$2$个$>$$10^6$的质因子,暴力做就好了。
//It is made by ljh2000
//有志者,事竟成,破釜沉舟,百二秦关终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
using namespace std;
typedef long long LL;
const int MAXN = 1000011;
int prime[MAXN],cnt;
bool vis[MAXN];
inline LL mul(LL x,LL y,LL mod){ LL r=0; while(y>0) { if(y&1) r+=x,r%=mod; x+=x; x%=mod; y>>=1; } return r; }
inline LL fast_pow(LL x,LL y,LL mod){ LL r=1; while(y>0) { if(y&1) r=mul(r,x,mod); x=mul(x,x,mod); y>>=1; } return r; }
inline LL getint(){
LL w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
} inline void init(){
for(int i=2;i<=1000000;i++) {
if(!vis[i]) { prime[++cnt]=i; }
for(int j=1;j<=cnt && prime[j]*i<=1000000;i++) {
vis[i*prime[j]]=1;
if(i%prime[j]==0) break;
}
}
} inline int Miller_Rabin(LL n){
if(n==1) return 0; if(n==2) return 1; if(!(n&1)) return 0;
int T=5,k=0; LL aa,nn=n-1,bb,cc;
while(!(nn&1)) nn>>=1,k++;
while(T--) {
aa=rand()%(n-1)+1;
bb=fast_pow(aa,nn,n);
for(int i=1;i<=k;i++) {
cc=mul(bb,bb,n);
if(cc==1 && bb!=1 && bb!=n-1) return 0;
bb=cc;
}
if(bb!=1) return 0;
}
return 1;
} inline void work(){
init();
while(1) {
LL n=getint(),m; if(n==-1) break;
bool flag=0; if(n==1 || n==2 || n==4) { printf("%I64d\n",n-1); continue; }
if(n%4==0) { printf("1\n"); continue; }
m=n; if(m%2==0) m>>=1;
for(int i=1;i<=cnt;i++) {
if(m%prime[i]==0) {
flag=1;
while(m%prime[i]==0) m/=prime[i];
break;
}
} if(flag==1) {
if(m==1) printf("%I64d\n",n-1);
else printf("1\n");
}
else {
if(Miller_Rabin(m)) printf("%I64d\n",n-1);
else {
LL kai=(LL)sqrt(m);
if(kai*kai==m) printf("%I64d\n",n-1);
else printf("1\n");
}
}
}
} int main()
{
work();
return 0;
}
//有志者,事竟成,破釜沉舟,百二秦关终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。
HDU4910 Problem about GCD的更多相关文章
- HDU 4910 Problem about GCD 找规律+大素数判断+分解因子
Problem about GCD Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 数学--数论--HDU 1792 A New Change Problem (GCD+打表找规律)
Problem Description Now given two kinds of coins A and B,which satisfy that GCD(A,B)=1.Here you can ...
- Leetcode: Water and Jug Problem && Summary: GCD求法(辗转相除法 or Euclidean algorithm)
You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...
- FZU2224 An exciting GCD problem 区间gcd预处理+树状数组
分析:(别人写的) 对于所有(l, r)区间,固定右区间,所有(li, r)一共最多只会有log个不同的gcd值, 可以nlogn预处理出所有不同的gcd区间,这样区间是nlogn个,然后对于询问离线 ...
- 2018.09.23 codeforces 1053A. In Search of an Easy Problem(gcd)
传送门 今天的签到题. 有一个很显然的结论,gcd(n∗m,k)≤2gcd(n*m,k)\le 2gcd(n∗m,k)≤2. 本蒟蒻是用的行列式求三角形面积证明的. 如果满足这个条件,就可以直接构造出 ...
- HDU 4910 HDOJ Problem about GCD BestCoder #3 第四题
首先 m = 1 时 ans = 0对于 m > 1 的 情况 由于 1 到 m-1 中所有和m互质的数字,在 对m的乘法取模 运算上形成了群 ai = ( 1<=a<m & ...
- HDU 5974"A Simple Math Problem"(GCD(a,b) = GCD(a+b,ab) = 1)
传送门 •题意 已知 $a,b$,求满足 $x+y=a\ ,\ LCM(x,y)=b$ 条件的 $x,y$: 其中,$a,b$ 为正整数,$x,y$ 为整数: •题解 关键式子:设 $a,b$ 为正整 ...
- [数论] hdu 5974 A Simple Math Problem (数论gcd)
传送门 •题意 一直整数$a,b$,有 $\left\{\begin{matrix}x+y=a\\ LCM(x*y)=b \end{matrix}\right.$ 求$x,y$ •思路 解题重点:若$ ...
- 2016 大连网赛---Different GCD Subarray Query(GCD离散+树状数组)
题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5869 Problem Description This is a simple probl ...
随机推荐
- 关闭在chrome里使用双指前进后退页面的功能
defaults write com.google.Chrome AppleEnableSwipeNavigateWithScrolls -bool FALSE
- GCJ 2015R2(Bilingual-最小割)
Problem C. Bilingual Problem Elliot's parents speak French and English to him at home. He has heard ...
- 基于Maven的SSM框架搭建
Maven + Spring + Spring MVC + Mybatis + MySQL整合SSM框架 1.数据库准备 本文主要想实现SSM框架的搭建,并基于该框架实现简单的登录功能,那么先新建一张 ...
- areas表-省市区
不全,缺少台湾省.香港.澳门:新疆重复了 /* Navicat MySQL Data Transfer Source Server : win7_local Source Server Version ...
- 0602-Zuul构建API Gateway-Zuul Http Client、cookie、header
一.Zuul Http Client zuul使用的默认HTTP客户端现在由Apache HTTP Client支持,而不是已弃用的Ribbon RestClient.要使用RestClient或使用 ...
- PAT 1049 Counting Ones [难]
1049 Counting Ones (30 分) The task is simple: given any positive integer N, you are supposed to coun ...
- 谷歌浏览器不能打开本地HTML文件
打开浏览器右上角菜单——>更多工具——>扩展程序——>将Axure RP Extension For Chrome 0.62.crx文件拖入——>成功后,勾选相关选项 文件的下 ...
- 奇异值与主成分分析(PCA)
主成分分析在上一节里面也讲了一些,这里主要谈谈如何用SVD去解PCA的问题.PCA的问题其实是一个基的变换,使得变换后的数据有着最大的方差.方差的大小描述的是一个变量的信息量,我们在讲一个东西的稳定性 ...
- C# DataTable Column DataType 对应 数据库
public DataTable MakeDataTable(){ DataTable myTable; DataRow myNewRow; // Create a new DataTable. ...
- LinQ高级查询、组合查询、IQueryable集合类型
LinQ高级查询: 1.模糊查询(包含) Repeater1.DataSource = con.car.Where(r =>r.name.Contains(s)).ToList(); 2.开头 ...