hdu5108枚举因子求最小的m
生活随笔
收集整理的這篇文章主要介紹了
hdu5108枚举因子求最小的m
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題意:
? ? ?給一個n(<=10Y),然后讓找到一個最小的m使得n/m是一個素數.
思路:
? ? ? 先用sqrt(n)的時間把所有的因子都求出來,然后在排序,枚舉,就行了,這個題目這么做的話,要是仔細算一下時間復雜度估計會跪,但是題目說大數據不多,這種說法的前提下一般臨街的時間復雜度都要試一試,還有就是在判斷素數和枚舉的時候寫的別太挫,不然很可能會超時。
#include<algorithm>
#include<stdio.h>
#include<math.h>
using namespace std;
int YZ[100000] ,yzs;
void DB(int now)
{
? ?yzs = 0;
? ?int max = (int)sqrt(now);
? ?for(int i = 1 ;i <= max ;i ++)
? ?{
? ? ? if(now % i == 0)?
? ? ? {
? ? ? ? ?YZ[++yzs] = i;
? ? ? ? ?YZ[++yzs] = now / i;
? ? ? }
? ?}
? ?if(max * max == now)
? ?yzs --;
}
bool jude(int now)
{
? ?int max = (int)sqrt(now);
? ?for(int i = 2 ;i <= max ;i ++)
? ?if(now % i == 0) return 0;
? ?return 1;
}
int main ()
{
? ?int n ,i;
? ?while(~scanf("%d" ,&n))
? ?{
? ? ? if(n <= 1)
? ? ? {
? ? ? ? ?printf("0\n");
? ? ? ? ?continue;
? ? ? }
? ? ? DB(n);
? ? ? sort(YZ + 1 ,YZ + yzs + 1);
? ? ? int mk = 0;
? ? ? for(i = 1 ;i <= yzs && !mk;i ++)
? ? ? {
? ? ? ? ?int now = n / YZ[i];
? ? ? ? ?if(jude(now))
? ? ? ? ?{
? ? ? ? ? ? printf("%d\n" ,YZ[i]);
? ? ? ? ? ? mk = 1;
? ? ? ? ?}
? ? ? }
? ? ? if(!mk) printf("0\n");
? ?}
? ?return 0;
}
? ? ? ? ? ??
? ? ?給一個n(<=10Y),然后讓找到一個最小的m使得n/m是一個素數.
思路:
? ? ? 先用sqrt(n)的時間把所有的因子都求出來,然后在排序,枚舉,就行了,這個題目這么做的話,要是仔細算一下時間復雜度估計會跪,但是題目說大數據不多,這種說法的前提下一般臨街的時間復雜度都要試一試,還有就是在判斷素數和枚舉的時候寫的別太挫,不然很可能會超時。
#include<algorithm>
#include<stdio.h>
#include<math.h>
using namespace std;
int YZ[100000] ,yzs;
void DB(int now)
{
? ?yzs = 0;
? ?int max = (int)sqrt(now);
? ?for(int i = 1 ;i <= max ;i ++)
? ?{
? ? ? if(now % i == 0)?
? ? ? {
? ? ? ? ?YZ[++yzs] = i;
? ? ? ? ?YZ[++yzs] = now / i;
? ? ? }
? ?}
? ?if(max * max == now)
? ?yzs --;
}
bool jude(int now)
{
? ?int max = (int)sqrt(now);
? ?for(int i = 2 ;i <= max ;i ++)
? ?if(now % i == 0) return 0;
? ?return 1;
}
int main ()
{
? ?int n ,i;
? ?while(~scanf("%d" ,&n))
? ?{
? ? ? if(n <= 1)
? ? ? {
? ? ? ? ?printf("0\n");
? ? ? ? ?continue;
? ? ? }
? ? ? DB(n);
? ? ? sort(YZ + 1 ,YZ + yzs + 1);
? ? ? int mk = 0;
? ? ? for(i = 1 ;i <= yzs && !mk;i ++)
? ? ? {
? ? ? ? ?int now = n / YZ[i];
? ? ? ? ?if(jude(now))
? ? ? ? ?{
? ? ? ? ? ? printf("%d\n" ,YZ[i]);
? ? ? ? ? ? mk = 1;
? ? ? ? ?}
? ? ? }
? ? ? if(!mk) printf("0\n");
? ?}
? ?return 0;
}
? ? ? ? ? ??
總結
以上是生活随笔為你收集整理的hdu5108枚举因子求最小的m的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hdu3415单调队列
- 下一篇: POJ1679判断最小生成树的唯一性