天天看點

HDU 1098 Ignatius's puzzle(數學歸納題or費馬小定理)

 Ignatius's puzzle

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 8427    Accepted Submission(s): 5843

Problem Description

Ignatius is poor at math,he falls across a puzzle problem,so he has no choice but to appeal to Eddy. this problem describes that:f(x)=5*x^13+13*x^5+k*a*x,input a nonegative integer k(k<10000),to find the minimal nonegative integer a,make the arbitrary integer x ,65|f(x)if

no exists that a,then print "no".

Input

The input contains several test cases. Each test case consists of a nonegative integer k, More details in the Sample Input.

Output

The output contains a string "no",if you can't find a,or you should output a line contains the a.More details in the Sample Output.

Sample Input

11

100

9999

Sample Output

22

no

43

題解:

題目大意:方程f(x)=5*x^13+13*x^5+k*a*x;輸入任意一個數k,是否存在一個數a,對任意x都能使得f(x)能被65整出。

現假設存在這個數a ,因為對于任意x方程都成立

是以,當x=1時f(x)=18+ka

又因為f(x)能被65整出,故設n為整數

可得,f(x)=n*65;

即:18+ka=n*65;

因為n為整數,若要方程成立

則問題轉化為,

對于給定範圍的a隻需要驗證,

是否存在一個a使得(18+k*a)%65==0

是以容易解得

注意,這裡a隻需到65即可

因為,當a==66時

也就相當于已經找了一個周期了,是以再找下去也找不到适當的a了

如果你非要證明的話,可以利用了取模過程與數的運算的次序上可交換原理簡單證明一下

本身看看就知道,這裡就不證了。。

AC代碼:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
using namespace std;
#define MA 10010
int main()
{
 int n,i,k;
 while(~scanf("%d",&k))
 {
     for(i=1;i<=65;i++)
     {
         if((18+i*k)%65==0)
           {
              printf("%d\n",i);
              break;
           }
     }
     if(i>=66)
        printf("no\n");
 }

    return 0;
}      

第二AC: 費馬小定理

#include <cstdio>
int main()
{
  int k,flag;
  while (scanf("%d",&k)!=EOF)
  {
    flag=0;
    for (int i=1;i<=65;i++)
    {
                     if (i*k%13==8&&i*k%5==2)
                   { 
                        flag=i;
            break;
                    }
    }
     if (flag)
    {
      printf("%d\n",flag);
    }
      else
     {
      printf("no\n");
     }
  }
}      

這一題是一個好題,涉及到費馬小定理!

什麼是費馬小定理呢?

費馬小定理(Fermat Theory)是數論中的一個重要定理,其内容為: 假如p是​​質數​​​,且Gcd(a,p)=1,那麼 a(p-1) ≡1(mod p)。即:假如a是整數,p是質數,且a,p互質(即兩者隻有一個​​公約數​​​1),那麼a的(p-1)​​次方​​​除以p的​​餘數​​恒等于1。該定理是1636年皮埃爾·德·費馬發現的。

是以我們要做的就是把這一題與費馬小定理聯系起來!

f(x)=5*x^13+13*x^5+k*a*x轉化成f(x)=(5*x^12+13*x^4+k*a)*x

1.當x=65的倍數時就行了;

2.當x=5的倍數時則需要(5*x^12+k*a)是13的倍數,由費馬小定理可知,因為x是5的倍數是以(x^(13-1)%13==1),是以(5*x^12%13==5),是以隻需要做到k*a%13==8即可!

3.同理可得當x=13的倍數時,隻需要做到(13*x^4+k*a)是5的倍數由費馬小定理可知,因為x是5的倍數是以(x^(5-1)%5==1),是以(13*x^(5-1)%5==3),是以隻需要做到k*a%5==2即可

4.當x不是上面的特殊數時,則需要f(x)=(5*x^12+13*x^4+k*a)*x被65整除,也就是需要同時滿足上面那兩個條件!