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% ...
随机推荐
- scrapy写爬虫是出现no module named win32api错误
windows下利用scrapy(python2.7)写爬虫,运行 scrapy crawl dmoz 命令时提示:exceptions.ImportError: No module named wi ...
- Android Studio 新建项目的R文件丢失的解决方法
最近Android Studio炒的比较热,于是笔者决定赶赶时髦,从Eclipse转到了Android Studio.不幸的是,用Android Studio创建项目的时候就遇到了一个比较尖锐的问题— ...
- Angulajs 定时器使用
在Angulajs的control中,不能利用 window.setInterval和 setTimeout 来操作定时器,因为这样做,虽然设置定时器会成功,但对$scope中的数据进行设置时,不会自 ...
- What is a good EPUB reader on Linux
Last updated on August 20, 2014 Authored by Adrien Brochard 12 Comments If the habit on reading book ...
- 基于Visual C++2013拆解世界五百强面试题--题18-程序结果分析2-终结篇
第二部分程序结果分析,分析流程还是写入代码注释中 分析下面程序的输出: #include <stdio.h> int main() { char *a = "hello" ...
- qt example
#include"mainwindow.h" #include"ui_mainwindow.h" #include<QWebFrame> #incl ...
- 工具篇-MAT(Memory Analyzer Tool)
--- layout: post title: 工具篇-MAT(Memory Analyzer Tool) description: 让内存泄漏无所遁形 2015-10-08 category: bl ...
- [译]Stairway to Integration Services Level 4 - 增量更新数据
在本文中, 我们说下增量更新数据:即将数据源中更新了的数据替换掉目标表中对应的数据. 更新代码 操作之前我们先把目标表e (dbo.Contact). 的数据改掉 Use AdventureWorks ...
- ppt类似工具AxeFile使用心得
一个所谓的傻瓜式过渡效果自动生成工具.定义好展示窗口大小,加入时序.所谓的闪烁效果,也是相当的滑稽. 是一个非专业的简单的快速PPT工具. 但是体现的确是扁平化的设计思路,很值得深思. ------ ...
- JUnit4的使用2
package com.imooc.test.aware; import org.junit.Test; import org.junit.runner.RunWith; import org.jun ...