zoukankan      html  css  js  c++  java
  • hdu1098费马小定理

    Ignatius's puzzle

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 9783    Accepted Submission(s): 6839


    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

    1、一开始读题,65|f(x)是什么意思都不清楚,最后百度才知道是f(x)能被65整除。

    2、而且写这题完全没有思路,数论不好,我是根据网上的思路写的。

    思路:
    则f(x+1 ) = f (x) +  5*( (13  1 ) x^12 ...... .....+(13  13) x^0  )+  13*(  (5  1 )x^4+...........+ ( 5  5  )x^0  )+k*a;

    很容易证明,除了5*(13  13) x^0 、13*( 5  5  )x^0 和k*a三项以外,其余各项都能被65整除.
    那么也只要求出18+k*a能被65整除就可以了.
    而f(1)也正好等于18+k*a:题目的关键是函数式f(x)=5*x^13+13*x^5+k*a*x;
    事实上,由于x取任何值都需要能被65整除.那么用数学归纳法.只需找到f(1)成立的a,并在假设f(x)成立的基础上,
    证明f(x+1)也成立.
    那么把f(x+1)展开,得到5*(  ( 13  0 )x^13 +  (13  1 ) x^12 ...... .....+(13  13) x^0)+13*(  ( 5  0 )x^5+(5  1 )x^4......其实就是二项式展开,这里就省略了  ......+ ( 5  5  )x^0  )+k*a*x+k*a;——————这里的( n  m)表示组合数,相信学过2项式定理的朋友都能看明白.

    然后提取出5*x^13+13*x^5+k*a*x。

    则f(x+1 ) = f (x) +  5*( (13  1 ) x^12 ...... .....+(13  13) x^0  )+  13*(  (5  1 )x^4+...........+ ( 5  5  )x^0  )+k*a;

    很容易证明,除了5*(13  13) x^0 、13*( 5  5  )x^0 和k*a三项以外,其余各项都能被65整除.
    那么也只要求出18+k*a能被65整除就可以了.
    而f(1)也正好等于18+k*a

    所以,只要找到a,使得18+k*a能被65整除,也就解决了这个题目. 

     假设存在这个数a,因为对于任意x方程都成立,所以,当x=1时f(x)=18+ka;有因为f(x)能被65整出,这可得出f(x)=n*65;

    即:18+ka=n*65;若该方程有整数解则说明假设成立。

    ax+by = c的方程有解的一个充要条件是:c%gcd(a, b) == 0。

     然后枚举直到65*n-18%k == 0为止。

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <string.h>
     4 using namespace std;
     5 
     6 int gcd(int a, int b)
     7 {
     8     return b == 0? a:gcd(b, a%b);
     9 }
    10 
    11 void swap(int &a, int &b)
    12 {
    13     int t = a;
    14     a = b;
    15     b = t;
    16 }
    17 
    18 int fun(int m, int n)
    19 {
    20     if(m < n) swap(m, n);
    21     gcd(m, n);
    22     if(!(18%gcd(m, n)))    return 1;
    23         return 0;
    24 }
    25 
    26 
    27 int main()
    28 {
    29     int m;
    30     while(~scanf("%d", &m))
    31     {
    32         if(fun(65, m))
    33         {
    34             for(int i = 1;; i++)
    35             {
    36                 if((i*65-18)%m == 0)
    37                 {
    38                     printf("%d
    ", (i*65-18)/m);
    39                     break;
    40                 }
    41             }
    42         }
    43         else printf("no
    ");
    44     }
    45     return 0;
    View Code

    思路2: 

    题意:给出k。求使得f(x)=5*x^13+13*x^5+k*a*x对任意x都为65的倍数的a的最小值。

    mark:65=13*5。要使f(x)是65的倍数,只需要f(x)是5和13的倍数即可。先来分析13的。

    若f(x)是13的倍数,

    有5*x^13+13*x^5+k*a*x % 13 == 0,其中13*x^5项显然不用考虑。

    则只需5*x^13 + k*a*x是13的倍数,即x*(5*x^12+k*a)是13的倍数。若x是13的倍数,不用考虑。

    若x不是13的倍数,则x一定与13互素,因为EulerPhi(13) == 12,从而x^12 % 13 == 1。

    所以可知5*x^12 % 13 == 5。

    因为要让任意x满足条件,则括号内必为13的倍数,有k*a+5 % 13 == 0,则k*a % 13 == 8。

    同理可得k*a % 5 == 2。

    据此,若k为5或13的倍数,a一定无解,否则,一定有解。

    根据k%5的结果,可能为1、2、3、4,a应分别取5n+2,5n+1,5n+4,5n+3。

    枚举a的值,若符合13的条件,则为解。

    费马小定理:

     

    费马小定理是数论中的一个重要定理,其内容为: 假如p是质数,且(a,p)=1,那么 a^(p-1) ≡1(mod p) 假如p是质数,且a,p互质,那么 a的(p-1)次方除以p的余数恒等于1 。

     1 #include <iostream>
     2 #include <cmath>
     3 #include <cstdio>
     4 using namespace std;
     5 int w[5]={0,2,1,4,3};
     6 int main()
     7 {
     8     int k,a;
     9     while(~scanf("%d",&k))
    10     {
    11         if(k%5==0||k%13==0)
    12             cout<<"no"<<endl;
    13         else
    14         {
    15             for(a=w[k%5];;a+=5)
    16             {
    17                 if(k*a%13==8)
    18                 {
    19                     cout<<a<<endl;
    20                     break;
    21                 }
    22             }
    23         }
    24     }
    25     return 0;
    26 }
  • 相关阅读:
    发布NBearV3最终测试版v3.2.5
    NBearV3教程——Web篇
    JUnit中的设计模式:命令模式
    HTTP协议 通信过程介绍
    JUnit中的设计模式:适配器模式
    《Head First设计模式》 读书笔记15 其余的模式(一) 桥接 生成器 责任链
    SQL基础:数据库规范化与三范式
    《Head First设计模式》 读书笔记13 复合模式 MVC模式
    Android Tab标签的使用基础
    Android设备上的传感器模拟工具:SensorSimulator
  • 原文地址:https://www.cnblogs.com/--lr/p/6921701.html
Copyright © 2011-2022 走看看