杭电ACM1098 Ignatius's puzzle

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.

题目大意:给定k,求a使得对于任意x,65|f(x)=5* x^13+13* x^5+k* a*x。

分析:
65=5 * 13,
先来看5|f(x)的情况:
5|5* x^13这是显然对任意x都成立的,我们只考虑后两项就可以了,
13%5=3,根据费马小定理 x^5%5=x,所以13* x^5%5=3*x%5,
推出(13* x^5+k* a * x)%5=(3+k * a) * x %5.
要使得对于任意x,x|f(x),则(3+k * a)%5=0。
再来看13|f(x)的情况:
和上面同样的分析,我们可以得到结果:(5+k * a)%13=0;
最后根据孙子定理知道:
k*a=65 * t+47,t为某个整数。我们只要从0一直试到64就可以知道满足条件的t(条件是指对给定的k,使得a为整数)为多少了。那么就知道a等于多少了。

import java.util.Scanner;

public class IgnatiusPuzzle {

    public static void main(String args[]){
        Scanner s=new Scanner(System.in);
        while(s.hasNext()){
            int k=s.nextInt();
            int n = 0,flag=0;
            for(int t=0;tif((65*t+47)%k==0){
                    n=(65*t+47)/k;
                    flag=1;
                    break;
                }
            }
            if(flag==0){
                System.out.println("no");
            }
            else{
                System.out.println(n);
            }
        }
    }
}

你可能感兴趣的:(Acm)