HDU-1098 Ignatius's puzzle

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

Input

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

Output

The output contains a string "no",if you can’t find a a a,or you should output a line contains the a 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}, 655x13+13x5+kax,xZ,就等价于
{ 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}. {513x5+kax135x13+kax,xZ.
这两个式子在当 5 ∣ x 5\mid x 5x 13 ∣ x 13\mid x 13x时显然成立,下面讨论 5 ∤ x 5\nmid x 5x 13 ∤ x 13 \nmid x 13x的情形。此时,该式转化为 { 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.. {513(x41)+13+ka135(x121)+5+ka.
由费马小定理, { 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. {5x41,5x13x121,13x
则该式继续等价为
{ 5 ∣ 13 + k a 13 ∣ 5 + k a , \left\{\begin{aligned}5 \mid 13+ka \\13 \mid 5+ka\end{aligned}\right., {513+ka135+ka,
则有 k a = 5 x − 13 = 13 y − 5 , x , y ∈ Z ka=5x-13=13y-5,x,y \in \mathbb{Z} ka=5x13=13y5,x,yZ解得 { 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}, x=113ty=15tka=1865t,tZ,
因而,本题转化为求解不定方程 a k + 65 t = − 18 , k , t ∈ Z ak+65t=-18,k,t\in \mathbb{Z} ak+65t=18,k,tZ
的解中 k k k的最小正值。
由拓展欧几里得算法,该方程有解的条件为 gcd ( a , 65 ) ∣ − 18 \text{gcd}(a,65)|-18 gcd(a,65)18 gcd ( 65 , − 18 ) = 1 \text{gcd}(65,-18)=1 gcd(65,18)=1所以要求 a a a必须满足 gcd ( a , 65 ) = 1 \text{gcd}(a,65)=1 gcd(a,65)=1当方程有解时,先求解出不定方程 a k + 65 t = 1 , k , t ∈ Z ak+65t=1,k,t\in \mathbb{Z} ak+65t=1,k,tZ的一个特解,再将其乘以 − 18 -18 18即为原方程的一个特解。然后再根据这个特解求 k 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). by0+(a%b)x0=gcd(b,a%b)ax0+b(y0bax0)=gcd(a,b).
当然,最后这一部分也可以用暴力法,在 1 1 1~ 65 65 65中寻找是否存在 k k k满足 65 ∣ a k + 18. 65 \mid ak+18. 65ak+18.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值