UVA10193 All You Need Is Love【进制+GCD】

”All you need is love. All you need is love.
All you need is love, love… love is all you need.”
The Beatles

There was invented a new powerfull gadget by the International Beautifull Machines corporation called the love machine! Given a string made of binary digits, the love machine answers if it’s made only of love, that is, if all you need is love to build that string. The definition of love for the love machine is another string of binary digits, given by a human operator. Let’s say we have a string L which represents “love” and we give a string S for the love machine. We say that all you need is love to build S, if we can repeatly subtract L from S until we reach L. The subtraction defined here is the same arithmetic subtra
    If S = L then S is obvious made of love.
    Let’s see an example. Supose S = “11011” and L = “11”. If we reapetly subtract L from S, we get: 11011, 11000, 10101, 10010, 1111, 1100, 1001, 110, 11. So, given this L, all you need is love to build S. Because of some limitations of the love machine, there can be no string with leading zeroes. That is, “0010101”, “01110101”, “011111”, etc. are invalid strings. Strings which have only one digit are also invalid (it’s another limitation).
    Your task in this problem is: given two valid binary strings, S1 and S2, find if it’s possible to have a valid string L such that both S1 and S2 can be made only of L (i.e. given two valid strings S1 and S2, find if there exists at least one valid string L such as both S1 and S2 are made only of L). For instance, for S1 = 11011 and S2 = 11000, we can have L = 11 such that S1 and S2 are both made only of L (as we can see in the example above).
Input
The first line of input is a positive integer N < 10000 which stands for the number of teste cases. Then, 2 ∗ N lines will follow. Each pair of lines consists in one teste case. Each line of the pair stands for each string (S1 and S2) to be entered as an input for the love machine. No string will have more than 30 characters. You can assume that all strings in the input will be valid acording to the rules above.
Output
For each string pair, you must print one of the following messages:
Pair #p: All you need is love!
Pair #p: Love is not all you need!
    Where p stands for the pair number (starting from 1). You should print the first message if there exists at least one valid string L such as both S1 and S2 can be made only of L. Otherwise, print the second line.
Sample Input
5
11011
11000
11011
11001
111111
100
1000000000
110
1010
100
Sample Output
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!

问题链接UVA10193 All You Need Is Love
问题简述:(略)
问题分析
    进制转换和GCD计算问题,不多解释。
    程序中使用了函数__gcd(),就不用自己写计算GCD的函数了。写一个适用于不同进制转换字符为整数的函数,相对来说会比较快一些。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA10193 All You Need Is Love */

#include <bits/stdc++.h>

using namespace std;

const int N = 30;
char s[N + 1];

int myatoi(char s[], int base)
{
    int ans = 0;
    for(int i = 0; s[i]; i++) {
        ans *= base;
        ans += s[i] - '0';
    }
    return ans;
}

int main()
{
    int t, s1, s2;
    scanf("%d", &t);
    for(int k = 1; k <= t; k++) {
        scanf("%s", s);
        s1 = myatoi(s, 2);
        scanf("%s", s);
        s2 = myatoi(s, 2);

        if(__gcd(s1, s2) > 1) printf("Pair #%d: All you need is love!\n", k);
        else printf("Pair #%d: Love is not all you need!\n", k);
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值