POJ 1142 质因数分解
只要很朴素的分解就可以了,数据量不大
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <stack>
#include <algorithm> #define ll long long
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXSIZE = ;
ll n;
stack <int> s; void init_prim(){
memset(visit, true, sizeof(visit));
int num = ;
for (int i = ; i <= nn; ++i){
if (visit[i] == true){
num++;
prime[num] = i;
}
for (int j = ; ((j <= num) && (i * prime[j] <= nn)); ++j){
visit[i * prime[j]] = false;
if (i % prime[j] == ) break; //点睛之笔
}
}
} ll quickpow(ll m,ll n,ll k){
int b = ;
while (n > ){
if (n & )
b = (b*m)%k;
n = n >> ;
m = (m*m)%k;
}
return b;
} ll getsum(int x){
int sum = ;
while(x){
sum += x % ;
x = x / ;
}
return sum;
} bool witness(ll a,ll n){
ll t,d,x;
d = ;
int i=ceil(log(n-1.0)/log(2.0)) - ;//j
for(;i>=;i--)
{
x=d; d=(d*d)%n;
if(d== && x!= && x!=n-) return true;
if( ((n-) & (<<i)) > )
d=(d*a)%n;
}
return d==? false : true;
}
bool miller_rabin(ll n){
int s[]={,,};
if(n== || n == ) return true;
if(n== || ((n&)==)) return false;
for(int i=;i<;i++)//
if(witness(s[i], n)) return false;
return true;
} bool isPrime(ll n){
if(n == || n == || n == || n == ) return true;
else if(n % == || n % == || n % == ) return false;
for(int i = ; i <= sqrt(n); ++i){
if(n % i == ) return false;
}
return true;
} bool judge(ll x){
int sum1, sum2 = , i;
sum1 = getsum(x);
for(i = ; i <= sqrt(x); ++i){
if(x % i == ){
s.push(i);
x = x / i;
while(x % i == ){
s.push(i);
x = x / i;
}
}
if(x == )
break;
}
if(x > ) s.push(x);
while(!s.empty()){
sum2 += getsum(s.top());
s.pop();
}
if(sum1==sum2) return true;
else return false;
}
int main(){
int i, j, k;
while(cin >> n){
if(n <= ) break;
ll num = n;
while(){
++num;
if(isPrime(num)) continue;
else if(judge(num)){
cout << num << endl;
break;
}
}
}
return ;
}
POJ 1142 质因数分解的更多相关文章
- POJ 1845 Sumdiv#质因数分解+二分
题目链接:http://poj.org/problem?id=1845 关于质因数分解,模板见:http://www.cnblogs.com/atmacmer/p/5285810.html 二分法思想 ...
- POJ 2429 long long 质因数分解
GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16206 Accepted: ...
- poj 3421 X-factor Chains——质因数分解
题目:http://poj.org/problem?id=3421 记忆化搜索竟然水过去了.仔细一想时间可能有点不对,但还是水过去了. #include<iostream> #includ ...
- Poj 1401 Factorial(计算N!尾数0的个数——质因数分解)
一.Description The most important part of a GSM network is so called Base Transceiver Station (BTS). ...
- POJ 1142 Smith Numbers(史密斯数)
Description 题目描述 While skimming his phone directory in 1982, Albert Wilansky, a mathematician of Leh ...
- poj 1811 大数分解
模板 #include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> ...
- algorithm@ 大素数判定和大整数质因数分解
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> #in ...
- 求n!质因数分解之后素数a的个数
n!质因数分解后P的个数=n/p+n/(p*p)+n/(p*p*p)+......直到n<p*p*p*...*p //主要代码,就这么点东西,数学真是厉害啊!幸亏我早早的就退了数学2333 do ...
- AC日记——质因数分解 1.5 43
43:质因数分解 总时间限制: 1000ms 内存限制: 65536kB 描述 已知正整数 n 是两个不同的质数的乘积,试求出较大的那个质数. 输入 输入只有一行,包含一个正整数 n. 对于60% ...
随机推荐
- COB工艺流程及基本要求
工艺流程及基本要求 清洁PCB---滴粘接胶---芯片粘贴---测试---封黑胶加热固化---测试---入库 1.清洁PCB 清洗后的PCB板仍有油污或氧化层等不洁部分用皮擦试帮定位或测试针位对擦拭的 ...
- rsync、ssh备份
Linux系统需求 为了实现这个备份程序,需要准备一个外部驱动器.您可以将备份写到一个外部USB磁盘,但这样做效率不高.因此,我假设您将备份写到位于网络某处的服务器中.这台服务器要为带有SSH和rsy ...
- MFC自绘控件学习总结
前言:从这学期开始就一直在学习自绘控件(mfc),目标是做出一款播放器界面,主要是为了打好基础,因为我基础实在是很烂....说说我自己心得体会以及自绘控件的方法吧,算是吐槽吧,说的不对和不全的地方,或 ...
- BZOJ 1874 取石子游戏 (NIM游戏)
题解:简单的NIM游戏,直接计算SG函数,至于找先手策略则按字典序异或掉,去除石子后再异或判断,若可行则直接输出. #include <cstdio> const int N=1005; ...
- 数学之路-python计算实战(16)-机器视觉-滤波去噪(邻域平均法滤波)
# -*- coding: utf-8 -*- #code:myhaspl@myhaspl.com #邻域平均法滤波,半径为2 import cv2 import numpy as np fn=&qu ...
- 1.自己写一个计算器demo
知识点: 1.System.Math.Pow() 实现乘方 2.实现计算器的运算优先级,依次调用的流程 问题: 还未实现“()”功能 解决方案 UI:
- 浅谈web前端开发阅历
WEB前端研发工程师,在国内算是一个朝阳职业,这个领域没有学校的正轨教育,大多数人都是靠本人自学成才.本文次要引见本人从事web开发以来(从大二至今)看过的书籍和本人的成长过程,目的是给想了解Java ...
- Java配置
JAVA_HOME -- JDK安装的路径 PATH -- 加入:;%JAVA_HOME%\bin; CLASSPATH -- 加入:;%JAVA_HOME%\lib\dt.jar;%JAVA_HOM ...
- linux命令:rsync, 同步文件和文件夹的命令
Usage: rsync [OPTION]... SRC [SRC]... DEST or rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST or ...
- 查看Oracle有哪些表或者视图
转自:http://www.2cto.com/database/201211/167577.html 1.查询当前用户下,有哪些表 Sql代码 SELECT * FROM user_tables ...