hdu 3864 素数分解

题意:求n是否只有4个因子,如果是就输出除1外的所有因子。

模板题,就不排版了

  1 #include<cstdio>
  2 #include<iostream>
  3 #include<algorithm>
  4 #include<cstring>
  5 #include<cmath>
  6 #include<queue>
  7 #include<map>
  8 #include<ctime>
  9 using namespace std;
 10 #define MOD 1000000007
 11 const int INF=0x3f3f3f3f;
 12 const double eps=1e-5;
 13 #define cl(a) memset(a,0,sizeof(a))
 14 #define ts printf("*****\n");
 15 const int MAXN=1005;
 16 int n,m,tt;
 17 const int S = 8; //随机算法判定次数,一般8~10就够了
 18 // 计算ret = (a*b)%c a,b,c < 2^63
 19 long long mult_mod(long long a,long long b,long long c)
 20 {
 21 a %= c;
 22 b %= c;
 23 long long ret = 0;
 24 long long tmp = a;
 25 while(b)
 26 {
 27 if(b & 1)
 28 {
 29 ret += tmp;
 30 if(ret > c)ret -= c;//直接取模慢很多
 31 }
 32 tmp <<= 1;
 33 if(tmp > c)tmp -= c;
 34 b >>= 1;
 35 }
 36 return ret;
 37 }
 38 // 计算 ret = (a^n)%mod
 39 long long pow_mod(long long a,long long n,long long mod)
 40 {
 41 long long ret = 1;
 42 long long temp = a%mod;
 43 while(n)
 44 {
 45 if(n & 1)ret = mult_mod(ret,temp,mod);
 46 temp = mult_mod(temp,temp,mod);
 47 n >>= 1;
 48 }
 49 return ret;
 50 }
 51 // 通过 a^(n-1)=1(mod n)来判断n是不是素数
 52 // n-1 = x*2^t 中间使用二次判断
 53 // 是合数返回true, 不一定是合数返回false
 54 bool check(long long a,long long n,long long x,long long t)
 55 {
 56 long long ret = pow_mod(a,x,n);
 57 long long last = ret;
 58 for(int i = 1;i <= t;i++)
 59 {
 60 ret = mult_mod(ret,ret,n);
 61 if(ret == 1 && last != 1 && last != n-1)return true;//合数
 62 last = ret;
 63 }
 64 if(ret != 1)return true;
 65 else return false;
 66 }
 67 //**************************************************
 68 // Miller_Rabin算法
 69 // 是素数返回true,(可能是伪素数)
 70 // 不是素数返回false
 71 //**************************************************
 72 bool Miller_Rabin(long long n)
 73 {
 74 if( n < 2)return false;
 75 if( n == 2)return true;
 76 if( (n&1) == 0)return false;//偶数
 77 long long x = n - 1;
 78 long long t = 0;
 79 while( (x&1)==0 ){x >>= 1; t++;}
 80 srand(time(NULL));/* *************** */
 81 for(int i = 0;i < S;i++)
 82 {
 83 long long a = rand()%(n-1) + 1;
 84 if( check(a,n,x,t) )
 85 return false;
 86 }
 87 return true;
 88 }
 89 //**********************************************
 90 // pollard_rho 算法进行质因素分解
 91 //
 92 //
 93 //*********************************************
 94 long long factor[100];//质因素分解结果(刚返回时时无序的)
 95 int tol;//质因素的个数,编号0~tol-1
 96 long long gcd(long long a,long long b)
 97 {
 98 long long t;
 99 while(b)
100 {
101 t = a;
102 a = b;
103 b = t%b;
104 }
105 if(a >= 0)return a;
106 else return -a;
107 }
108 //找出一个因子
109 long long pollard_rho(long long x,long long c)
110 {
111 long long i = 1, k = 2;
112 srand(time(NULL));
113 long long x0 = rand()%(x-1) + 1;
114 long long y = x0;
115 while(1)
116 {
117 i ++;
118 x0 = (mult_mod(x0,x0,x) + c)%x;
119 long long d = gcd(y - x0,x);
120 if( d != 1 && d != x)return d;
121 if(y == x0)return x;
122 if(i == k){y = x0; k += k;}
123 }
124 }
125 //对 n进行素因子分解,存入factor. k设置为107左右即可
126 void findfac(long long n,int k)
127 {
128 if(n == 1)return;
129 if(Miller_Rabin(n))
130 {
131 factor[tol++] = n;
132 return;
133 }
134 long long p = n;
135 int c = k;
136 while( p >= n)
137 p = pollard_rho(p,c--);//值变化,防止死循环k
138 findfac(p,k);
139 findfac(n/p,k);
140 }
141 //POJ 1811
142 //给出一个N(2 <= N < 2^54),如果是素数,输出"Prime",否则输出最小的素因子
143 int main()
144 {
145     int T;
146     long long n;
147     #ifndef ONLINE_JUDGE
148     freopen("1.in","r",stdin);
149     #endif
150     while(scanf("%I64d",&n)==1)
151     {
152         if(n==1)
153         {
154             printf("is not a D_num\n");
155             continue;
156         }
157         tol=0;
158         findfac(n,107);
159         if(tol!=2 && tol!=3)
160         {
161             printf("is not a D_num\n");
162             continue;
163         }
164         sort(factor,factor+tol);
165         if(tol==2)
166         {
167             if(factor[0]!=factor[1])
168             {
169                 printf("%I64d %I64d %I64d\n",factor[0],factor[1],factor[0]*factor[1]);
170                 continue;
171             }
172             else
173             {
174                 printf("is not a D_num\n");
175                 continue;
176             }
177         }
178         if(tol==3)
179         {
180             if(factor[0]==factor[1]&&factor[1]==factor[2])
181             {
182                 printf("%I64d %I64d %I64d\n",factor[0],factor[0]*factor[1],factor[0]*factor[1]*factor[2]);
183                 continue;
184             }
185             else
186             {
187                 printf("is not a D_num\n");
188                 continue;
189             }
190         }
191     }
192     return 0;
193 }

 

转载于:https://www.cnblogs.com/cnblogs321114287/p/4470443.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/459445.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

QQuickRenderControl

2019独角兽企业重金招聘Python工程师标准>>> http://doc.qt.io/qt-5/qquickrendercontrol.html http://translate.google.com.hk/translate?hlzh-CN&slauto&tlen&uhttp%3A%2F%2Fhabrahabr.ru%2Fpost%2F247477%2F http://www.kdab.com/overview-qt3d-2-…

线程的控制(创建、等待、终止)、分离线程

一、线程控制 1、线程&#xff1a;线程是资源调度的基本单位&#xff0c;线程是进程内部的一个执行流&#xff0c;在进程的地址空间内运行。在Linux 下没有真正意义上的线程&#xff0c;线程是用进程模拟的&#xff0c;又被称为轻量级进程。 2、由于同⼀一进程的多个线程共享同…

从netty-example分析Netty组件

分析netty从源码开始 准备工作&#xff1a; 1.下载源代码&#xff1a;https://github.com/netty/netty.git 我下载的版本为4.1 2. eclipse导入maven工程。 netty提供了一个netty-example工程&#xff0c; 分类如下&#xff1a; Fundamental Echo ‐ the very basic client and …

$GLOBALS -- 变量

可以在$GLOBALS中获得所有的变量 $GLOBALS天然就是一个有很多内容的变量 $_SERVER $jackson "Example content";$_GET[A] A;$_GET[B] B; 于是乎 就有了 $GLOBALS[A]$GLOBALS[B]$GLOBALS[jackson]转载于:https://www.cnblogs.com/qinqiu/p/4475836.html

背景图片适应屏幕百分百

<!DOCTYPE html> <html><head><meta charset"utf-8"><meta name"viewport" content"widthdevice-width, initial-scale1" /><title>背景图片大小</title> </head><style>body{margin: 0;…

20150504-日报

1、Delphi中的存储过程 参数 数据类型Delphi7中的使用存储过程的话&#xff0c;加入要获取输入参数的话&#xff0c;一般都是通过这样的方式&#xff1a;with spDelRights do begin if Active then Close; Parameters.Clear; Parameters.Refresh; Parameters.ParamByName(usern…

cep

cep posted on 2015-12-16 17:03 秦瑞It行程实录 阅读(...) 评论(...) 编辑 收藏 转载于:https://www.cnblogs.com/ruiy/p/5051673.html

BZOJ-1036 [ZJOI2008]树的统计

树链剖分模版题。 #include <cstdlib> #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <cctype> #include <cmath> #define rep(i, l, r) for(int il; i<r; i) #define clr(x, c) mem…

malloc/free 和 new/delete的联系和区别

一、malloc/free 1、 函数原型&#xff1a;void* malloc(longNumBytes) 该函数分配了NumBytes个字节&#xff0c;并返回了只想这块空间的的指针。如果分配失败则返回空。 函数原型&#xff1a;Void free(void *firstBytes) 该函数是将之前用malloc分配的内存空间释放&#…

RHCS集群原理概述

一、 什么是RHCSRHCS是Red Hat Cluster Suite的缩写&#xff0c;也就是红帽集群套件&#xff0c;RHCS是一个能够提供高可用性、高可靠性、负载均衡、存储共享且经济廉价的集群工具集合&#xff0c;它将集群系统中三大集群架构融合一体&#xff0c;可以给web应用、数据库应用等提…

Linux学习笔记11——文件I/O之二

一、文件共享 内核使用三种数据结构表示打开的文件&#xff0c;它们之间的关系决定了在文件共享方面一个进程对另一个进程可能产生的影响。 1、每个进程在进程表中都有一个记录项&#xff0c;记录项中包含有一张打开文件描述表  2、内核为所有打开文件维持一张文件表  3、每…

死锁产生的原因及条件、如何避免死锁

一、死锁的定义 是指两个或两个以上的进程在执行过程中&#xff0c;由于竞争资源或者由于彼此通信而造、成的一种阻塞的现象&#xff0c;若无外力作用&#xff0c;它们都将无法推进下去。此时称系统处于死锁状态或系统产生了死锁&#xff0c;这些永远在互相等待的进程称为死锁…

Git Proxy开关

2019独角兽企业重金招聘Python工程师标准>>> 这个是配合ShadowSocks使用的&#xff0c;在~/.bash_aliases或者~/.bash_profile中设置以下代码&#xff1a; #git proxy enable alias gitpe"git config --global http.proxy socks5://127.0.0.1:1080;git config …

SAP web 开发 (第二篇 bsp 开发 mvc模式 Part2 )

单击第一个图标&#xff0c;第一个图标突出显示&#xff0c;单击第二个图标&#xff0c;第一个变灰&#xff0c;第二个突出显示&#xff0c;反之一样。单击history读取历史记录。 Controller ZCL_SUS_C_ORDER_CHANGE 1. DO_INITmethod DO_INIT. *CALL METHOD SUPER->DO_I…

cuda内存总结

&#xff11;&#xff0e;shared memory __shared__ 声明为共享内存&#xff0c;将会保存在共享内存中 &#xff12;&#xff0e;constant memory __constant__ 声明为常量内存&#xff0c;将会保存在常量内存中&#xff0c;常量内存是只读内存&#xff0c;声明时要静态的分配…

平衡二叉查找树插入节点操作( AVLTree ):旋转、调整平衡

AVL树的插入 在向一棵本来高度平衡的AVL树中插入一个新节点时&#xff0c;如果树中某个结点的平衡因子的绝对值 > 1&#xff0c;则出现了不平衡。设新插入结点为P&#xff0c;从结点P到根节点的路径上&#xff0c;每个结点为根的子树的高度都可能增加1&#xff0c;因此在每…

Fork/Join框架介绍

转http://www.infoq.com/cn/articles/fork-join-introduction/ 1. 什么是Fork/Join框架 Fork/Join框架是Java7提供了的一个用于并行执行任务的框架&#xff0c; 是一个把大任务分割成若干个小任务&#xff0c;最终汇总每个小任务结果后得到大任务结果的框架。 我们再通过Fork和…

为什么析构函数可以能声明为虚函数,构造函数不可以

转自&#xff1a;http://blog.csdn.NET/chen825919148/article/details/8020550 构造函数不能声明为虚函数&#xff0c;析构函数可以声明为虚函数&#xff0c;而且有时是必须声明为虚函数。 不建议在构造函数和析构函数里面调用虚函数。 构造函数不能声明为虚函数的原因是: 1 …

【DFS】NYOJ-325-zb的生日

【题目链接&#xff1a;NYOJ-325】 一道以我名字命名的题目&#xff0c;难道要我生日的时候再A&#xff1f; 思路&#xff1a;依旧深搜&#xff0c;但这个问题应该有一个专有名词吧&#xff0c;看别的博客说是 “容量为 sum/2 的背包问题”&#xff0c;不懂。。。 1 // abs() …

Ubuntu Sudo 无法解析的主机

如果对ubuntu在安装时候的主机名称不满意&#xff0c;可以使用如下的方法进行修改 需要注意的是如果只修改其中一个&#xff0c;使用sudo的时候会报“无法解析主机名称的”错误 1、进入etc目录&#xff0c;使用cat查看hosts文件 alloyubuntu:/etc$ cat hosts -n 1 127.0.0.1 …