題解/算法 {202. 最幸运的数字}

Link

Solution

Suppose the problem is solvable and let the answer be x x x, then 888... % L = 0 888... \% L = 0 888...%L=0

888... ≡ 0 ( % L ) 888... \equiv 0 (\% L) 888...0(%L)

8 ∗ 1 0 x − 1 9 = k ∗ L 8 * \frac{10^x - 1}{9} = k * L 8910x1=kL (where k k k is unknown, it is a certain integer k > 0 k > 0 k>0 due to x > 0 x >0 x>0)

1 0 x − 1 = k ∗ 9 ∗ L 8 10^x - 1 = \frac{k * 9 * L}{8} 10x1=8k9L (Note that, the right hand fraction must be a integer > 0 > 0 >0 due to k > 0 k > 0 k>0, and k ∗ 9 ∗ L 8 ≠ k ∗ 9 ∗ L 8 \frac{k * 9 * L}{8} \neq k * \frac{9 * L}{8} 8k9L=k89L according to 整数分式的分解)
. Cuz k k k is unknown, we should take it out of the formula; Let g = G C D ( 9 ∗ L , 8 ) g = GCD(9*L, 8) g=GCD(9L,8) and 8 = g ∗ t 8 = g * t 8=gt, then k ∗ 9 ∗ L 8 = k t ∗ 9 ∗ L g \frac{k * 9 * L}{8} = \frac{k}{t} * \frac{9 * L}{g} 8k9L=tkg9L where the right-hand two fraction must be integer; now let k 1 = k t k_1 = \frac{k}{t} k1=tk be the unknown.

1 0 x − 1 = k 1 ∗ 9 ∗ L g 10^x - 1 = k_1 * \frac{9 * L}{g} 10x1=k1g9L ( k 1 k_1 k1 is a certain integer)

1 0 x − 1 ≡ 0 ( % 9 ∗ L g ) 10^x - 1 \equiv 0 (\% \frac{9 * L}{g}) 10x10(%g9L) (According to 将等式转换为同余式)

Finally, 1 0 x ≡ 1 ( % 9 ∗ L g ) 10^x \equiv 1 (\% \frac{9 * L}{g}) 10x1(%g9L)

For generality, a x ≡ 1 ( % M ) a^x \equiv 1 (\% M) ax1(%M)
+ Solvable if-and-only-if a , M a,M a,M are co-prime;
+ According to 最小正幂为欧拉函数的约数, iterating all divisors of x ∣ ϕ ( M ) x | \phi(M) xϕ(M), and using Binary-Exponentiation to test whether a x % M a^x \% M ax%M equals 1 1 1;

Code

L;
M = 9 * L / GCD(9 * L, 8);
if( GCD( M, 10) != 1){
	return "Unsolvable";
}
Phi = $( euler_function of `M`);
for( `x` : all divisors of `Phi` increasingly){
	if( $10 ^ x (% M)$ == 1){
		return "answer is x";
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值