hdu 3864 素数分解
题意:求n是否只有4个因子,如果是就输出除1外的所有因子。
模板题,就不排版了
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<ctime>
using namespace std;
#define MOD 1000000007
const int INF=0x3f3f3f3f;
const double eps=1e-;
#define cl(a) memset(a,0,sizeof(a))
#define ts printf("*****\n");
const int MAXN=;
int n,m,tt;
const int S = ; //随机算法判定次数,一般8~10就够了
// 计算ret = (a*b)%c a,b,c < 2^63
long long mult_mod(long long a,long long b,long long c)
{
a %= c;
b %= c;
long long ret = ;
long long tmp = a;
while(b)
{
if(b & )
{
ret += tmp;
if(ret > c)ret -= c;//直接取模慢很多
}
tmp <<= ;
if(tmp > c)tmp -= c;
b >>= ;
}
return ret;
}
// 计算 ret = (a^n)%mod
long long pow_mod(long long a,long long n,long long mod)
{
long long ret = ;
long long temp = a%mod;
while(n)
{
if(n & )ret = mult_mod(ret,temp,mod);
temp = mult_mod(temp,temp,mod);
n >>= ;
}
return ret;
}
// 通过 a^(n-1)=1(mod n)来判断n是不是素数
// n-1 = x*2^t 中间使用二次判断
// 是合数返回true, 不一定是合数返回false
bool check(long long a,long long n,long long x,long long t)
{
long long ret = pow_mod(a,x,n);
long long last = ret;
for(int i = ;i <= t;i++)
{
ret = mult_mod(ret,ret,n);
if(ret == && last != && last != n-)return true;//合数
last = ret;
}
if(ret != )return true;
else return false;
}
//**************************************************
// Miller_Rabin算法
// 是素数返回true,(可能是伪素数)
// 不是素数返回false
//**************************************************
bool Miller_Rabin(long long n)
{
if( n < )return false;
if( n == )return true;
if( (n&) == )return false;//偶数
long long x = n - ;
long long t = ;
while( (x&)== ){x >>= ; t++;}
srand(time(NULL));/* *************** */
for(int i = ;i < S;i++)
{
long long a = rand()%(n-) + ;
if( check(a,n,x,t) )
return false;
}
return true;
}
//**********************************************
// pollard_rho 算法进行质因素分解
//
//
//*********************************************
long long factor[];//质因素分解结果(刚返回时时无序的)
int tol;//质因素的个数,编号0~tol-1
long long gcd(long long a,long long b)
{
long long t;
while(b)
{
t = a;
a = b;
b = t%b;
}
if(a >= )return a;
else return -a;
}
//找出一个因子
long long pollard_rho(long long x,long long c)
{
long long i = , k = ;
srand(time(NULL));
long long x0 = rand()%(x-) + ;
long long y = x0;
while()
{
i ++;
x0 = (mult_mod(x0,x0,x) + c)%x;
long long d = gcd(y - x0,x);
if( d != && d != x)return d;
if(y == x0)return x;
if(i == k){y = x0; k += k;}
}
}
//对 n进行素因子分解,存入factor. k设置为107左右即可
void findfac(long long n,int k)
{
if(n == )return;
if(Miller_Rabin(n))
{
factor[tol++] = n;
return;
}
long long p = n;
int c = k;
while( p >= n)
p = pollard_rho(p,c--);//值变化,防止死循环k
findfac(p,k);
findfac(n/p,k);
}
//POJ 1811
//给出一个N(2 <= N < 2^54),如果是素数,输出"Prime",否则输出最小的素因子
int main()
{
int T;
long long n;
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
#endif
while(scanf("%I64d",&n)==)
{
if(n==)
{
printf("is not a D_num\n");
continue;
}
tol=;
findfac(n,);
if(tol!= && tol!=)
{
printf("is not a D_num\n");
continue;
}
sort(factor,factor+tol);
if(tol==)
{
if(factor[]!=factor[])
{
printf("%I64d %I64d %I64d\n",factor[],factor[],factor[]*factor[]);
continue;
}
else
{
printf("is not a D_num\n");
continue;
}
}
if(tol==)
{
if(factor[]==factor[]&&factor[]==factor[])
{
printf("%I64d %I64d %I64d\n",factor[],factor[]*factor[],factor[]*factor[]*factor[]);
continue;
}
else
{
printf("is not a D_num\n");
continue;
}
}
}
return ;
}
hdu 3864 素数分解的更多相关文章
- hdu 5317 合数分解+预处理
RGCDQ Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- HDU_3071 Gcd & Lcm game 【素数分解 + 线段树 + 状压】
一.题目 Gcd & Lcm game 二.分析 非常好的一题. 首先考虑比较暴力的做法,肯定要按区间进行处理,对于$lcm$和$gcd$可以用标准的公式进行求,但是求$lcm$的时候是肯定 ...
- HDU 3864 D_num Miller Rabin 质数推断+Pollard Rho大整数分解
链接:http://acm.hdu.edu.cn/showproblem.php? pid=3864 题意:给出一个数N(1<=N<10^18).假设N仅仅有四个约数.就输出除1外的三个约 ...
- [hdu 6069]素数筛+区间质因数分解
给[L,R]区间的每一个数都质因数分解的复杂度可以达到(R-L)logR,真的涨姿势…… 另外,质因数分解有很重要的一点,就是只需要打sqrt(R)以内的素数表就够了……因为超过sqrt(R)的至多只 ...
- 数论 - 组合数学 + 素数分解 --- hdu 2284 : Solve the puzzle, Save the world!
Solve the puzzle, Save the world! Problem Description In the popular TV series Heroes, there is a ta ...
- HDU 4344 大数分解大素数判定
这里贴个模板吧.反正是不太理解 看原题就可以理解用法!! #include <cstdio> #include <iostream> #include <algorith ...
- hdu 5104 素数打表水题
http://acm.hdu.edu.cn/showproblem.php?pid=5104 找元组数量,满足p1<=p2<=p3且p1+p2+p3=n且都是素数 不用素数打表都能过,数据 ...
- hdu4497 GCD and LCM ——素数分解+计数
link:http://acm.hdu.edu.cn/showproblem.php?pid=4497 如果G%L != 0,说明一定无解. 把K = G / L质数分解,G / L = p1^t1 ...
- hdu 2012 素数判定 Miller_Rabbin
素数判定 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
随机推荐
- oracle环境变量详解
共享存储文件系统(NFS) 通常情况下,ORACLE_SID这个环境变量全称Oracle System Identifier,,用于在一台服务器上标识不同的实例,默认情况下,实例名就是ORACLE_S ...
- JavaScript 金额、数字、千分位、千分位、保留几位小数、舍入舍去、支持负数
JavaScript 金额.数字 千分位格式化.保留指定位数小数.支持四舍五入.进一法.去尾法 字段说明: number:需要处理的数字: decimals:保留几位小数,默认两位,可不传: dec_ ...
- Shell-输入密码转换为*
Code: read -p "请输入使用者都名称:" USER echo -e "请输入使用者密码: \c" while : ;do char=` #这里是反引 ...
- Linux内核抢占实现机制分析【转】
Linux内核抢占实现机制分析 转自:http://blog.chinaunix.net/uid-24227137-id-3050754.html [摘要]本文详解了Linux内核抢占实现机制.首先介 ...
- javascript多投事件的处理 (转)
出处 http://blog.csdn.net/dead_of_winter/article/details/1646367 尽管ecma标准指定了addEventListener这样的方法来实现事件 ...
- java基础60 JavaScript字符串转换成数字(网页知识)
1.字符串转换成数字 <!doctype html> <html> <head> <meta charset="utf-8"> &l ...
- CF401D 【Roman and Numbers】
题意将n(n<=10^18)的各位数字重新排列(不允许有前导零) 求 可以构造几个mod m等于0的数字解法状压f[S][k] 表示选用的位数集合为S,mod m 为k的方案数注意不能有前导 ...
- Vue select 下拉菜单
1.html <div id="app-8"> <select v-model="selected"> <option v-for ...
- Visual Studio 2013百度云下载地址
Visual Studio 2013百度云下载地址: 链接: https://pan.baidu.com/s/1JkVYLnFo2TWSu4dkDdZP3Q 提取码: 关注公众号[获取 winf ...
- 003 Ajax中传输格式为XML
一: 1.优缺点 二:大纲 1.结构设计 三:程序 1.xml <?xml version="1.0" encoding="utf-8"?> < ...