1. 程式人生 > >hrbust 1328 相等的最小公倍數【暴力打表找規律】

hrbust 1328 相等的最小公倍數【暴力打表找規律】

相等的最小公倍數
Time Limit: 1000 MS Memory Limit: 65536 K
Total Submit: 486(156 users) Total Accepted: 185(133 users) Rating:  Special Judge: No
Description

定義An12n的最小公倍數,例如,A1 = 1A2 = 2A3 = 6A4 = 12A5 = 60A6 = 60

請你判斷對於給出的任意整數nAn是否等於An – 1

Input

本題有多組測試資料,輸入的第一行是一個整數T代表著測試資料的數量,接下來是T組測試資料。

對於每組測試資料:

1 包含一個整數n (2 ≤ n ≤ 106)

Output

對於每組測試資料:

1 如果An等於An-1則輸出YES否則輸出NO

Sample Input

1

6

Sample Output

YES

Source
哈理工2012春季校賽熱身賽 2012.04.03
Author
齊達拉圖@HRBUST

思路:

暴力打表,找規律。

首先我們暴力處理出來前n<=40的YES解:


然後發現,如果對應是YES的解,那麼一定能在從2~n-1中找到兩個數,使其互質(gcd==1)相乘能夠得到n。

2、那麼對應,如果能夠從2~n-1中找到兩個數,使其互質(gcd==1)相乘能夠得到n,那麼輸出YES,否則輸出NO.

暴力程式碼:

#include<stdio.h>
#include<string.h>
using namespace std;
#define ll __int64
ll lc[510];
ll gcd(ll x,ll y)
{
    if(x%y==0)return y;
    else return gcd(y,x%y);
}
ll lcm(ll x,ll y)
{
    return x*y/gcd(x,y);
}
int main()
{
    for(ll n=1;n<=40;n++)
    {
        ll output=1;
        for(ll i=1;i<=n;i++)
        {
            output=lcm(output,i);
        }
        lc[n]=output;
       printf("%I64d\n",output);
    }
    for(int i=1;i<=40;i++)
    {
        if(lc[i]==lc[i-1])
        {
            printf("%d\n",i);
        }
    }
}


Ac程式碼:

#include<stdio.h>
#include<string.h>
using namespace std;
int gcd(int x,int y)
{
    if(x%y==0)return y;
    else return gcd(y,x%y);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        int flag=0;
        for(int i=2;i<n;i++)
        {
            if(n%i==0)
            {
                int u=i;int v=n/i;
                if(gcd(u,v)==1)
                {
                    flag=1;
                    printf("YES\n");
                    break;
                }
            }
        }
        if(flag==0)printf("NO\n");
    }
}