本文版权归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的更多相关文章

  1. HDU 4910 Problem about GCD 找规律+大素数判断+分解因子

    Problem about GCD Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  2. 数学--数论--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 ...

  3. 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 ...

  4. FZU2224 An exciting GCD problem 区间gcd预处理+树状数组

    分析:(别人写的) 对于所有(l, r)区间,固定右区间,所有(li, r)一共最多只会有log个不同的gcd值, 可以nlogn预处理出所有不同的gcd区间,这样区间是nlogn个,然后对于询问离线 ...

  5. 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. 本蒟蒻是用的行列式求三角形面积证明的. 如果满足这个条件,就可以直接构造出 ...

  6. HDU 4910 HDOJ Problem about GCD BestCoder #3 第四题

    首先 m = 1 时 ans = 0对于 m > 1 的 情况 由于 1 到 m-1 中所有和m互质的数字,在 对m的乘法取模 运算上形成了群 ai = ( 1<=a<m & ...

  7. 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$ 为正整 ...

  8. [数论] hdu 5974 A Simple Math Problem (数论gcd)

    传送门 •题意 一直整数$a,b$,有 $\left\{\begin{matrix}x+y=a\\ LCM(x*y)=b \end{matrix}\right.$ 求$x,y$ •思路 解题重点:若$ ...

  9. 2016 大连网赛---Different GCD Subarray Query(GCD离散+树状数组)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5869 Problem Description This is a simple probl ...

随机推荐

  1. Apache的访问控制

      目录配置段 注释不能写在指令后面,下面这样是不行的,应当换行,但为了阅读方便我就这么写了 Alias /dir/  "/var/www/html/admin"      #路径 ...

  2. Python高级特性(3): Classes和Metaclasses(转)

    原文:Python高级特性(3): Classes和Metaclasses 类和对象 类和函数一样都是Python中的对象.当一个类定义完成之后,Python将创建一个“类对象”并将其赋值给一个同名变 ...

  3. 解决hung_task_timeout_secs问题【方法待校验】

    问题描述:   kernel: "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this messag ...

  4. python和shell之间变量的相互调用

    python -> shell: 1.环境变量 2.字符串连接 3.通过管道 import os var=’123’ os.popen(’wc -c’, ’w’).write(var) 4.通过 ...

  5. Servlet实现前后端交互的原理及过程解析

    在日常调试项目时,总是利用tomcat去启动项目,并进行前后端联调,但对于前后端的请求响应的交互原理及过程并不是特别清晰. 为什么在前端发出相应请求,就能跳转到后端通过程序得到结果再响应到前端页面呢? ...

  6. 批处理delims分割时遇到的问题。。

    今天写了个将文件每行按逗号分割并取第六行的批处理.但是结果不对.看图一目了然. for 循环的/f 后面的参数是这样的 然后文件的内容是这样的 亮点是倒数第二行..其实6才是第六列的值.其他行第六列都 ...

  7. php微信支付接口开发程序(流程已通)

    php微信支付接口开发程序(流程已通) 来源:未知    时间:2014-12-11 17:11   阅读数:11843   作者:xxadmin [导读] 微信支付接口现在也慢慢的像支付宝一个可以利 ...

  8. libxml2 在mingw中 xmlfree连接错误问题

    libxml2 在mingw中 xmlfree连接错误问题 2013年10月02日 ⁄ 综合 ⁄ 共 1527字 ⁄ 字号 小 中 大 ⁄ 评论关闭 原地址:http://blog.csdn.net/ ...

  9. 001-搭建spring boot项目

    1.第一步.file--new--project. 2.spring initializr--project sdk--default--next 3. 4.spring boot--选择依赖项--n ...

  10. Python3 计算城市距离

    利用上一篇得到的城市经纬度算城市距离 import requests from math import radians, cos, sin, asin, sqrt def geocode(addres ...