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% ...
随机推荐
- python安装集成包
anaconda, 包含各种科学运算包以及astropy.装完它一劳永逸. https://www.continuum.io/downloads
- The Standard Librarian: I/O and Function Objects: Containers of Pointers
C++ Experts Forum The Standard Librarian: I/O and Function Objects: Containers of Pointers Matthew A ...
- 浅谈USB设备的VID和PID
根据USB规范的规定,所有的USB设备都有供应商ID(VID)和产品识别码(PID),主机通过不同的VID和PID来区别不同的设备,VID和PID都是两个字节长,其中,供应商ID(VID)由供应商向U ...
- AssetManager中的路径参数不能包含"assets/"
String path = “music/bg.mp3”: //正确的参数 //String path = “assets/music/bg.mp3”: //错误的参数 //String path = ...
- [译]SSRS 编写带参数的MDX报表
编写MDX报表长久以来对于报表人员来说都比较痛苦. 当然如果你用查询设计器(Query Designer) 直接拖拉数据集那就很方便,但是你们有没有想过查询设计器是怎么创建MDX的.或者创建的参数是如 ...
- PHP自练项目中个人中心创建,修改,验证(服务器端和客户端验证)
当注册成功到登录后进入个人中心,查看和修改自己的资料 第一步:创建个人中心: <?php //定义个常量,用来授权调用includes里面的文件 define('IN_TG',true); // ...
- python中的异常如何处理
一.异常基础 在编程程序出现bug时一般不会将错误信息显示给用户,而是现实一个提示的页面. try: #正常逻辑代码 input = raw_input("输入数字:") data ...
- [LeetCode]题解(python):009-Palindrome Number
题目来源: https://leetcode.com/problems/palindrome-number/ 题意分析: 这题是要判断一个int是否一个回文数,要求不能申请额外的空间. 题目思路: 这 ...
- js基本框架
- float与position
使用float会使块级元素的宽高表现为包裹内容(在不设定宽高的情况下) 这是当然的 我们使用float就是使几个div排在一行 当然不可能在宽度上撑满父元素啦 至于高度 不论有没有float 高 ...