CF173A Rock-Paper-Scissors 题解
阅读原文时间:2023年07月08日阅读:1

有 \(2\) 个人在玩石头剪刀布,已知他们的出手都有一定的规律,求 \(n\) 局之后两个人各输了几局。

数据范围:\(1\leqslant n\leqslant 2\times 10^9\),周期长度不超过 \(10^3\)。

一个一个模拟肯定不现实,会 TLE,所以我们应当考虑是否有更快速的方法。

我们设第一个人的周期长度为 \(a_1\),第二个人的周期长度为 \(a_2\),则很容易发现,在 每 \(\operatorname{lcm}(a_1,a_2)\) 局里面,他们输的局数是固定的,因为他们正好能够做完整数个周期,而且可以证明 \(\operatorname{lcm}(a_1,a_2)<10^6\),所以我们考虑将一个周期里面每局的情况都统计出来,如果能凑出 \(\operatorname{lcm}(a_1,a_2)\) 局出来,就计算有多少个 \(\operatorname{lcm}(a_1,a_2)\) 局,显然有 \(\left\lfloor\dfrac{n}{\operatorname{lcm}(a_1,a_2)}\right\rfloor\) 个,直接拿两个人在每 \(\operatorname{lcm}(a_1,a_2)\) 局里面输的次数乘上 \(\left\lfloor\dfrac{n}{\operatorname{lcm}(a_1,a_2)}\right\rfloor\),再加上他们剩下来的几局中的输的次数即可。

string s1, s2;
int n, loses[1000007], loset[1000007], lose1, lose2, ans1, ans2;

inline int gcd(int a, int b) {return !b ? a : gcd(b, a % b);}
inline int lcm(int a, int b) {return a / gcd(a, b) * b;} //NOIP2020T1 血的教训

int main() {
    n = Rint; str(s1, len1); str(s2, len2);
    int round = lcm(len1, len2);
    F(i, 0, round - 1) {
        if((s1[i % len1] == 'R' && s2[i % len2] == 'P') || (s1[i % len1] == 'P' && s2[i % len2] == 'S') || (s1[i % len1] == 'S' && s2[i % len2] == 'R'))
            loses[i + 1]++, lose1++;
        if((s2[i % len2] == 'R' && s1[i % len1] == 'P') || (s2[i % len2] == 'P' && s1[i % len1] == 'S') || (s2[i % len2] == 'S' && s1[i % len1] == 'R'))
            loset[i + 1]++, lose2++;
    }
    F(i, 1, n % round) ans1 += loses[i], ans2 += loset[i];
    return printf("%d %d", ans1 + lose1 * (n / round), ans2 + lose2 * (n / round)), 0;
}