zoukankan      html  css  js  c++  java
  • hdu 3864 D_num

    思路:给一个数n,是否只有4个约数(包括1),也就是找3个大于1的约数。

    而任何一个数都可由质数表示,所以对于给定的数,只需要进行质因数分解。这里有

    2种情况:如果有3个一样的质因数,则满足条件;否则只需要2个不同的质因子。

    代码如下:

     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<algorithm>
     4 #include<iomanip>
     5 #include<cmath>
     6 #include<cstring>
     7 #include<vector>
     8 #define ll __int64
     9 #define pi acos(-1.0)
    10 #define MAX 5000001
    11 using namespace std;
    12 ll n,e[5];
    13 int prime[MAX],cnt;
    14 bool f[MAX];
    15 void init()
    16 {
    17     int i,j;
    18     cnt=0;
    19     for(i=2;i<MAX;i++){
    20         if(f[i]==0) prime[cnt++]=i;
    21         for(j=0;j<cnt&&i*prime[j]<MAX;j++){
    22             f[i*prime[j]]=1;
    23             if(i%prime[j]==0) break;
    24         }
    25     }
    26 }
    27 void solve()
    28 {
    29     ll i;
    30     ll num=n,k=0;
    31     for(i=0;i<cnt&&prime[i]*prime[i]<=n;i++){
    32         while(n%prime[i]==0){
    33             e[k++]=prime[i];
    34             n/=prime[i];
    35         }
    36         if(k>3){
    37             cout<<"is not a D_num"<<endl;
    38             return;
    39         }
    40     }
    41     if(n>1) e[k++]=n;
    42     if(k>3||k<2){
    43         cout<<"is not a D_num"<<endl;
    44         return;
    45     }
    46     else if(k==3){
    47         if(e[0]==e[1]&&e[1]==e[2]){
    48             cout<<e[0]<<' '<<e[0]*e[1]<<' '<<num<<endl;
    49             return;
    50         }
    51         else {
    52             cout<<"is not a D_num"<<endl;
    53             return;
    54         }
    55     }
    56     else{
    57         if(e[0]==e[1]){
    58             cout<<"is not a D_num"<<endl;
    59             return;
    60         }
    61         else{
    62             cout<<e[0]<<' '<<e[1]<<' '<<num<<endl;
    63             return;
    64         }
    65     }
    66 }
    67 int main(){
    68     init();
    69     while(cin>>n){
    70         solve();
    71     }
    72     return 0;
    73 }
    View Code
  • 相关阅读:
    字符串分割并按大小排序
    为人之气
    RMQ
    线段树
    算法中的数学
    动态规划(DP)
    图的表示
    广度优先搜索(BFS)
    深度优先搜索(DFS)
    MyBatis(3.2.3)
  • 原文地址:https://www.cnblogs.com/xin-hua/p/3239796.html
Copyright © 2011-2022 走看看