【解題】d306. 10193 - All You Need Is Love

題目概述

有一種規則,當 S 能夠被某個 L 不斷減去,最後的到了 L 本身就是 Love。

求解

給予兩個 S1 和 S2 字串,判斷是否存在一個 L。

程式碼

點我展開

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// File Name: d306.cpp
// Created on: 2024-10-28 23:04
#include <bits/stdc++.h>
#define ouo ios_base::sync_with_stdio(false), cin.tie(0)
#define ll long long
#define db double
#define pii pair<int, int>
#define pdd pair<double, double>
using namespace std;

int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);

int n;
cin >> n;
for (int t = 1; t <= n; t++) {
string s1, s2;
cin >> s1 >> s2;

int n1 = 0, n2 = 0;
for (int i = 0; i < s1.size(); i++) {
n1 = n1 << 1;
n1 += s1[i] - '0';
}
for (int i = 0; i < s2.size(); i++) {
n2 = n2 << 1;
n2 += s2[i] - '0';
}

int tmp, a = max(n1, n2), b = min(n1, n2);
while (tmp = a % b) {
a = b;
b = tmp;
}
if (b > 1)
cout << "Pair #" << t << ": All you need is love!\n";
else
cout << "Pair #" << t << ": Love is not all you need!\n";
}
}
標籤(Tags)

CPE UVa GCD