本文版权归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;
}
//有志者,事竟成,破釜沉舟,百二秦关终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。

  

最新文章

  1. xamarin优化listView.ScrollTo
  2. Linux C/C++ Memory Leak Detection Tool
  3. Spring基础—— Bean 的作用域
  4. zoj 3057 Beans Game 博弈论
  5. switch……case不能匹配字符串的方法 .xml
  6. 【转】Android 布局学习之——LinearLayout属性baselineAligned的作用及baseline
  7. iOS菜鸟之AFN的二次封装
  8. C++ primer学习方法
  9. oracle 优化 —— 分区表
  10. Deep Learning(深度学习)学习笔记整理系列之(一)
  11. modelsim+win环境下systemverilog调用c函数
  12. cmd命令报4048错误
  13. PowerBi利用Python Script绕过ODBC来导入MongoDB数据
  14. WPF: 共享Grid宽度或高度的方法
  15. VS2017 SVN插件-AnkhSVN
  16. Java 内部类的作用
  17. Spring Bean的ref属性和IoC注入集合
  18. win10安装mysql一直卡在最后一步进行不下去
  19. Linux Shell脚本面试25个经典问答
  20. 【转发】Python使用openpyxl读写excel文件

热门文章

  1. 病毒侵袭---hdu2896(AC自动机)
  2. mytest3.py-api接入平台获取数据
  3. livego
  4. php与oracle11g经典分页
  5. awk的常用操作场景以及工作中涉及到的一些场景实例
  6. SDUT2857:艺术联合会(简单dp)
  7. PKU 1573 Robot Motion(简单模拟)
  8. zookeeper No route to host
  9. iOS 自动订阅开发
  10. Linux下多线程下载工具myget