杭电OJ Problem-1098

Ignatius’s puzzle

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

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 f(x)=5x^{13}+13x^5+kax , input a nonegative integer k ( k < 10000 ) k(k<10000) , to find the minimal nonegative integer a a , make the arbitrary integer x x , 65 f ( x ) 65\mid f(x) .
if no exists that a a ,then print "no".

Input

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

Output

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

Sample Input

11
100
9999

Sample Output

22
no
43

Reference Code

#include<bits/stdc++.h>
using namespace std;
int a,k,t;
void exgcd(int a,int b,int &x,int &y){
    if (!b) x=1,y=0;
    else exgcd(b,a%b,y,x),y-=a/b*x;
}
int main(){
    while(~scanf("%d",&a)){
        if (!(a%5)||!(a%13))
            printf("no\n");
        else{
            exgcd(a,65,k,t);
            k=(k*(-18)%65+65)%65;
            printf("%d\n",k);
        }
    }
    return 0;
}

Tips

看了Discuss里面的解答,似乎都不够严谨,笔者试着写一下自己的过程。
题目中要求 65 5 x 13 + 13 x 5 + k a x , x Z , 65 \mid 5x^{13}+13x^5+kax,\forall x\in \mathbb{Z}, 就等价于
{ 5 13 x 5 + k a x 13 5 x 13 + k a x , x Z . \left\{\begin{aligned}5 \mid 13x^5+kax \\13 \mid 5x^{13 }+kax\end{aligned}\right.,\forall x\in \mathbb{Z}.
这两个式子在当 5 x 5\mid x 13 x 13\mid x 时显然成立,下面讨论 5 x 5\nmid x 13 x 13 \nmid x 的情形。此时,该式转化为 { 5 13 ( x 4 1 ) + 13 + k a 13 5 ( x 12 1 ) + 5 + k a . \left\{\begin{aligned}&amp;5 \mid 13(x^4-1)+13+ka \\&amp;13 \mid 5(x^{12 }-1)+5+ka\end{aligned}\right..
由费马小定理, { 5 x 4 1 , 5 x 13 x 12 1 , 13 x \left\{\begin{aligned}&amp;5 \mid x^4-1,5\nmid x\\&amp;13 \mid x^{12}-1,13\nmid x\end{aligned}\right.
则该式继续等价为
{ 5 13 + k a 13 5 + k a , \left\{\begin{aligned}5 \mid 13+ka \\13 \mid 5+ka\end{aligned}\right.,
则有 k a = 5 x 13 = 13 y 5 , x , y Z ka=5x-13=13y-5,x,y \in \mathbb{Z} 解得 { x = 1 13 t y = 1 5 t k a = 18 65 t , t Z , \left\{\begin{aligned}&amp;x=-1-13t \\&amp;y=-1-5t\\&amp;ka=-18-65t\end{aligned}\right.,t \in \mathbb{Z},
因而,本题转化为求解不定方程 a k + 65 t = 18 , k , t Z ak+65t=-18,k,t\in \mathbb{Z}
的解中 k k 的最小正值。
由拓展欧几里得算法,该方程有解的条件为 gcd ( a , 65 ) 18 \text{gcd}(a,65)|-18 gcd ( 65 , 18 ) = 1 \text{gcd}(65,-18)=1 所以要求 a a 必须满足 gcd ( a , 65 ) = 1 \text{gcd}(a,65)=1 当方程有解时,先求解出不定方程 a k + 65 t = 1 , k , t Z ak+65t=1,k,t\in \mathbb{Z} 的一个特解,再将其乘以 18 -18 即为原方程的一个特解。然后再根据这个特解求 k k 的最小正值。
至于特解的求解,可以利用
b y 0 + ( a % b ) x 0 = gcd ( b , a % b ) a x 0 + b ( y 0 a b x 0 ) = gcd ( a , b ) . by_0+(a\%b)x_0=\gcd(b,a\%b)\\ \Leftrightarrow ax_0+b(y_0-\lfloor \frac{a}{b} \rfloor x_0)=\gcd(a,b).
当然,最后这一部分也可以用暴力法,在 1 1 ~ 65 65 中寻找是否存在 k k 满足 65 a k + 18. 65 \mid ak+18.

猜你喜欢

转载自blog.csdn.net/qq_43549984/article/details/86653801