IBM(International Beautiful Machines)公司發明了一種小玩意兒叫做「愛的算命機」。這台機器會回答你是否非常渴望愛情。這機器運作的情形是:請你輸入一僅含 0 和 1 的字串(稱為 S),機器自己則定義一僅含 0 和 1 的字串(稱為 L,Love 的意思)。然後機器不斷的用 S 去減 L(當然是 2 進位的減法),如果最後可以得到 S = L,代表 S 是用 Love 做成的。如果最後 L > S,代表 S 不是用 Love 做成的。
舉例說明:假設 S = “11011”,L = “11”。如果我們不斷的從 S 減去 L,我們可以得到:11011、11000、10101、10010、1111、1100、1001、110、11。所以我們得到 L 了,也就是 S 是用 Love 做的。由於愛的算命機的某些限制,字串不可以有以 0 為開頭的,也就是說 “0010101”、“01110101”、“011111” 這些字串都是不合法的。另外,只有一個位元的字串也是不合法的。
Pair #1: All you need is love! Pair #2: Love is not all you need! Pair #3: Love is not all you need! Pair #4: All you need is love! Pair #5: All you need is love!
解題思路:
題目在說要不斷的做 S - L,直到找到一個合法的 L。
所謂合法的 L 就是 S = L。(找 S 是否為 L 的倍數)
若要用一數找出同時對於兩字串都合法的情形,就是用到 GCD。
將二進位字串 S1 S2 轉成十進位 a b(用 stoi(str, nullptr, 2)),判斷 a 與 b 的最大公因數(GCD)是否大於 1。
intgcd(int a, int b){ while (b != 0){ int r = a % b; a = b; b = r; } return a; }
intmain(){ ios::sync_with_stdio(false), cin.tie(nullptr); int N; cin >> N; string s1, s2; for (int p = 1; p <= N; ++p){ cin >> s1 >> s2; int a = stoi(s1, nullptr, 2); int b = stoi(s2, nullptr, 2); cout << "Pair #" << p << ": "; cout << ( gcd(a, b) > 1 ? "All you need is love!" : "Love is not all you need!"); cout << '\n'; } return0; }
intmain(){ longlong n, m; while (cin >> n >> m){ if (m <= 1 || n < m) { cout << "Boring!\n"; continue; } if (n == 1) { cout << "Boring!\n"; continue; }
vector<longlong> nums; bool isBoring = false;
while (true) { nums.emplace_back(n); if (n == 1) break; if (n % m != 0) { isBoring = true; break; } n /= m; }
if (isBoring) { cout << "Boring!\n"; } else { for (size_t i = 0; i < nums.size(); ++i) { if (i > 0) cout << " "; cout << nums[i]; } cout << '\n'; } } return0; }
boolis_prime(int x){ for (int y = 2; y * y <= x; ++y){ if (x % y == 0){ returnfalse; } } returntrue; }
intmain(){ ios::sync_with_stdio(false), cin.tie(nullptr); string N; while (cin >> N){ int a = stoi(N); reverse(N.begin(), N.end()); int b = stoi(N); if (is_prime(a)){ if (a != b && is_prime(b)){ cout << a << " is emirp."; } else{ cout << a << " is prime."; } } else{ cout << a << " is not prime."; } cout << "\n"; } return0; }
intdigitSum(const string& N){ int sum = 0; for (char c : N){ sum += c - '0'; } return sum; }
intmain(){ ios::sync_with_stdio(false), cin.tie(nullptr); string N; while (cin >> N && N != "0"){ int degree = 0; int sum = digitSum(N); degree++; while (sum >= 10){ sum = digitSum(to_string(sum)); degree++; } if (sum == 9) { cout << N << " is a multiple of 9 and has 9-degree " << degree << "." << '\n'; } else { cout << N << " is not a multiple of 9." << '\n'; } } return0; }
G=0; for(i=1;i<N;i++) for(j=i+1;j<=N;j++) { G+=GCD(i,j); } /*Here GCD() is a function that finds the greatest common divisor of the two input numbers*/
範例程式碼:
實測 Uva Online Judge 跟 Zerojudge 都過得了,主打一個能過就行。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include<bits/stdc++.h>
usingnamespace std;
intmain(){ ios::sync_with_stdio(false), cin.tie(nullptr); int N; while (cin >> N && N != 0){ int G = 0; for (int i = 1; i < N; ++i){ for (int j = i + 1; j <= N; ++j){ G += __gcd(i, j); // C++ 14 } } cout << G << '\n'; } return0; }
// 檢查中心 (r, c) 為中心、邊長為 len 的正方形是否合法 boolis_valid(const vector<string>& grid, int r, int c, int len, int M, int N){ char ch = grid[r][c]; // 中心點的字元 int half = len / 2; // 擴張範圍的一半(如邊長 3,擴張距離是 1) // half 有點像半徑的概念
// 邊界檢查 if (r - half < 0 || r + half >= M || c - half < 0 || c + half >= N) returnfalse;
// 檢查範圍內所有字元是否與中心相同 for (int i = r - half; i <= r + half; ++i) { for (int j = c - half; j <= c + half; ++j) { if (grid[i][j] != ch) returnfalse; } }
while (cin >> p[0].x >> p[0].y >> p[1].x >> p[1].y >> p[2].x >> p[2].y >> p[3].x >> p[3].y) { Point A, B, C; if (p[0] == p[2]) { A = p[0]; B = p[1]; C = p[3]; } elseif (p[0] == p[3]) { A = p[0]; B = p[1]; C = p[2]; } elseif (p[1] == p[2]) { A = p[1]; B = p[0]; C = p[3]; } else { A = p[1]; B = p[0]; C = p[2]; }
今為公元 2200 年。科學在兩百年間進步了許多。提到兩百年是因為這個問題是利用時間機器送回到公元 2000 年。現在可以建立人與電腦 CPU 之間的直接連結。人們可以透過 3D 顯示器(也就是今天的螢幕)來觀看他人的夢境,就像看電影一樣。本世紀的一個問題是人們對電腦變得過度依賴,以致於他們的分析能力幾乎接近於零。電腦現在可以閱讀問題並自動解決它們,但只能解決困難的問題。現在沒有簡單的問題了。我們的首席科學家遇到了大麻煩,因為他忘記了組合鎖的密碼。出於安全考量,現今的電腦無法解決和組合鎖相關的問題。在一個仲夏夜,科學家做了一個夢,夢見許多無號整數四處飛舞。他利用他的電腦記錄下這些數字,然後他得到線索,如果這些數字是( X₁,X₂,...,Xₙ ),他必須找到一個整數 A ( A 是組合鎖密碼),使得