2020牛客暑期多校训练营(第一场)F. Infinite String Comparision

链接

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=1031233

把字符串当做 26 26 26 进制数,将串 S S S 化为无限循环“小数”: s 2 6 ∣ s ∣ − 1 \frac{s}{26^{|s|}-1} 26s1s
则有:

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>t26s1s>26t1ts26t+t>t26s+sst>ts

另外两种情况同理

官方题解

根据 Periodicity Lemma 可得,若 s ∞ s^{\infty} s t ∞ t^{\infty} t 的前 ∣ s ∣ + ∣ t ∣ − g c d ( ∣ s ∣ , ∣ t ∣ ) |s|+|t|-gcd(|s|,|t|) s+tgcd(s,t) 位都相同,则 s ∞ = t ∞ s^{\infty}=t^{\infty} s=t

代码

#include
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;
}

你可能感兴趣的:(数学——杂项)