链接
https://ac.nowcoder.com/acm/contest/5666/F
题意
比较字符串 a ∞ a^\infty a∞, b ∞ b^\infty b∞ 的大小
思路
类比 10 10 10 进制中无限循环小数,如 0.233233... = 233 999 = 233 1 0 3 − 1 0.233233...=\frac{233}{999}=\frac{233}{10^3-1} 0.233233...=999233=103−1233
把字符串当做
26
26
26 进制数,将串
S
S
S 化为无限循环“小数”:
s
2
6
∣
s
∣
−
1
\frac{s}{26^{|s|}-1}
26∣s∣−1s
则有:
s ∞ > t ∞ ⇔ s 2 6 ∣ s ∣ − 1 > t 2 6 ∣ t ∣ − 1 ⇔ s ⋅ 2 6 ∣ t ∣ + t > t ⋅ 2 6 ∣ s ∣ + s ⇔ s t ‾ > t s ‾ s^{\infty}>t^{\infty}\Leftrightarrow\frac{s}{26^{|s|}-1}>\frac{t}{26^{|t|}-1}\Leftrightarrow s\cdot26^{|t|}+t>t\cdot26^{|s|}+s\Leftrightarrow\overline{st}>\overline{ts} s∞>t∞⇔26∣s∣−1s>26∣t∣−1t⇔s⋅26∣t∣+t>t⋅26∣s∣+s⇔st>ts
另外两种情况同理
官方题解
根据 Periodicity Lemma 可得,若 s ∞ s^{\infty} s∞ 和 t ∞ t^{\infty} t∞ 的前 ∣ s ∣ + ∣ t ∣ − g c d ( ∣ s ∣ , ∣ t ∣ ) |s|+|t|-gcd(|s|,|t|) ∣s∣+∣t∣−gcd(∣s∣,∣t∣) 位都相同,则 s ∞ = t ∞ s^{\infty}=t^{\infty} s∞=t∞
代码
#include<bits/stdc++.h>
using namespace std;
string a,b;
int main() {
while(cin>>a>>b) {
string c=a+b,d=b+a;
if(c>d) puts(">");
else if(c<d) puts("<");
else puts("=");
}
return 0;
}