zoukankan      html  css  js  c++  java
  • 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 = 10100000;
    ll n;
    stack <int> s;
    
    void init_prim(){
        memset(visit, true, sizeof(visit));
        int num = 0;
        for (int i = 2; i <= nn; ++i){
            if (visit[i] == true){
                num++;
                prime[num] = i;
            }
            for (int j = 1; ((j <= num) && (i * prime[j] <= nn));  ++j){
                visit[i * prime[j]] = false;
                if (i % prime[j] == 0) break; //点睛之笔
            }
        }
    }
    
    ll quickpow(ll m,ll n,ll k){
        int b = 1;
        while (n > 0){
              if (n & 1)
                 b = (b*m)%k;
              n = n >> 1 ;
              m = (m*m)%k;
        }
        return b;
    }
    
    ll getsum(int x){
        int sum = 0;
        while(x){
            sum += x % 10;
            x = x / 10;
        }
        return sum;
    }
    
    bool witness(ll a,ll n){
        ll t,d,x;
        d = 1;
        int i=ceil(log(n-1.0)/log(2.0)) - 1;//j
        for(;i>=0;i--)
        {
            x=d;  d=(d*d)%n;
            if(d==1 && x!=1 && x!=n-1) return true;
            if( ((n-1) & (1<<i)) > 0)
                d=(d*a)%n;
        }
        return d==1? false : true;
    }
    bool miller_rabin(ll n){
        int s[]={2,7,61};
        if(n==2 || n == 1)    return true;
        if(n==1 || ((n&1)==0))    return false;
        for(int i=0;i<3;i++)// 3
            if(witness(s[i], n))    return false;
        return true;
    }
    
    bool isPrime(ll n){
        if(n == 1 || n == 2 || n == 3 || n == 5) return true;
        else if(n % 2 == 0 || n % 3 == 0 || n % 5 == 0)  return false;
        for(int i = 2; i <= sqrt(n); ++i){
            if(n % i == 0) return false;
        }
        return true;
    }
    
    bool judge(ll x){
        int sum1, sum2 = 0, i;
        sum1 = getsum(x);
        for(i = 2; i <= sqrt(x); ++i){
            if(x % i == 0){
                s.push(i);
                x = x / i;
                while(x % i == 0){
                    s.push(i);
                    x = x / i;
                }
            }
            if(x == 1)
                break;
        }
        if(x > 1) 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 <= 0)  break;
            ll num = n;
            while(1){
                ++num;
                if(isPrime(num))    continue;
                else if(judge(num)){
                    cout << num << endl;
                    break;
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    Rafy 框架
    巧用拦截器:高效的扩展点设计
    Rafy 框架
    Rafy 框架
    Rafy 框架
    Rafy 框架-发布网页版用户手册
    Rafy 领域实体框架
    基金投资方法札记
    股票、基金投资方案总结
    BaaS API 设计规范
  • 原文地址:https://www.cnblogs.com/wushuaiyi/p/3878155.html
Copyright © 2011-2022 走看看