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
<
10000
)
k(k<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)
65∣f(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},
65∣5x13+13x5+kax,∀x∈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∣13x5+kax13∣5x13+kax,∀x∈Z.
这两个式子在当
5
∣
x
5\mid x
5∣x和
13
∣
x
13\mid x
13∣x时显然成立,下面讨论
5
∤
x
5\nmid x
5∤x和
13
∤
x
13 \nmid x
13∤x的情形。此时,该式转化为
{
5
∣
13
(
x
4
−
1
)
+
13
+
k
a
13
∣
5
(
x
12
−
1
)
+
5
+
k
a
.
\left\{\begin{aligned}&5 \mid 13(x^4-1)+13+ka \\&13 \mid 5(x^{12 }-1)+5+ka\end{aligned}\right..
{5∣13(x4−1)+13+ka13∣5(x12−1)+5+ka.
由费马小定理,
{
5
∣
x
4
−
1
,
5
∤
x
13
∣
x
12
−
1
,
13
∤
x
\left\{\begin{aligned}&5 \mid x^4-1,5\nmid x\\&13 \mid x^{12}-1,13\nmid x\end{aligned}\right.
{5∣x4−1,5∤x13∣x12−1,13∤x
则该式继续等价为
{
5
∣
13
+
k
a
13
∣
5
+
k
a
,
\left\{\begin{aligned}5 \mid 13+ka \\13 \mid 5+ka\end{aligned}\right.,
{5∣13+ka13∣5+ka,
则有
k
a
=
5
x
−
13
=
13
y
−
5
,
x
,
y
∈
Z
ka=5x-13=13y-5,x,y \in \mathbb{Z}
ka=5x−13=13y−5,x,y∈Z解得
{
x
=
−
1
−
13
t
y
=
−
1
−
5
t
k
a
=
−
18
−
65
t
,
t
∈
Z
,
\left\{\begin{aligned}&x=-1-13t \\&y=-1-5t\\&ka=-18-65t\end{aligned}\right.,t \in \mathbb{Z},
⎩⎪⎨⎪⎧x=−1−13ty=−1−5tka=−18−65t,t∈Z,
因而,本题转化为求解不定方程
a
k
+
65
t
=
−
18
,
k
,
t
∈
Z
ak+65t=-18,k,t\in \mathbb{Z}
ak+65t=−18,k,t∈Z
的解中
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,t∈Z的一个特解,再将其乘以
−
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(y0−⌊ba⌋x0)=gcd(a,b).
当然,最后这一部分也可以用暴力法,在
1
1
1~
65
65
65中寻找是否存在
k
k
k满足
65
∣
a
k
+
18.
65 \mid ak+18.
65∣ak+18.