跳到主要內容

UVA10193

Problem F: All You Need Is Love 



"All you need is love. All you need is love.
All you need is love, love... love is all you need."
The Beatles

The Problem

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 subtraction in base 2. By this definition it's easy to see that if L > S (in binary), then S is not made of love. 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).

The 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.

The 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!




解法: 先轉成十進制後 使用輾轉相除法






import java.util.Scanner;

public class UVA10193 {

 public static void main(String[] args) {

  Scanner sc = new Scanner(System.in);

  int count = Integer.parseInt(sc.nextLine());

  int pair = 1;

  for (int i = 0; i < count; i++) {

   int S1 = Integer.parseInt(sc.nextLine(), 2);
   int S2 = Integer.parseInt(sc.nextLine(), 2);

   int temp = 0;
   // Euclidean algorithm
   while (S1 % S2 != 0) {

    temp = S1 % S2;
    S1 = S2;
    S2 = temp;

   }

   System.out.println(S2 != 1 ? "Pair #" + pair
     + ": All you need is love!" : "Pair #" + pair
     + ": Love is not all you need!");

   pair++;
  }

  sc.close();
 }

}

留言

這個網誌中的熱門文章

UVA11349

J - Symmetric Matrix Time Limit: 1 sec Memory Limit: 16MB You`re given a square matrix M. Elements of this matrix are M ij : {0 < i < n, 0 < j < n}. In this problem you'll have to find out whether the given matrix is symmetric or not. Definition: Symmetric matrix is such a matrix that all elements of it are non-negative and symmetric with relation to the center of this matrix. Any other matrix is considered to be non-symmetric. For example: All you have to do is to find whether the matrix is symmetric or not. Elements of a matrix given in the input are -2 32  <= M ij  <= 2 32  and 0 < n <= 100. INPUT: First line of input contains number of test cases T <= 300. Then T test cases follow each described in the following way. The first line of each test case contains n - the dimension of square matrix. Then n lines follow each of then containing row i. Row contains exactly n elements separated by a space character. j-th number in row i i...

UVA11461

A square number is an integer number whose square root is also an integer. For example 1, 4, 81 are some square numbers. Given two numbers a and b you will have to find out how many square numbers are there between a and b (inclusive). Input The input file contains at most 201 lines of inputs. Each line contains two integers a and b (0 < a ≤ b ≤ 100000). Input is terminated by a line containing two zeroes. This line should not be processed. Output For each line of input produce one line of output. This line contains an integer which denotes how many square numbers are there between a and b (inclusive). Sample Input 1 4 1 10 0 0 Sample Output 2 3 大意:給兩個數字 求範圍內平方不大於第二個數字的數量 解法: 以最大數取根號後往回看 import java.util.Scanner; public class UVA11461 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int start = sc.nextInt(); int last = sc.nextInt(); if (last == 0) break; System.o...

UVA11005

Problem B Cheapest Base Input:  Standard Input Output:  Standard Output When printing text on paper we need ink. But not every character needs the same amount of ink to print: letters such as 'W', 'M' and '8' are more expensive than thinner letters as ' i ', 'c' and '1'. In this problem we will evaluate the cost of printing numbers in several bases. As you know, numbers can be expressed in several different bases. Well known bases are binary (base 2; digits 0 and 1), decimal (base 10; digits 0 to 9) and hexadecimal (base 16; digits 0 to 9 and letters A to F). For the general base  n  we will use the first  n  characters of the string "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", which means the highest base in this problem is 36. The lowest base is of course 2. Every character from this string has an associated cost, represented by an integer value between 1 and 128. The cost to print a number in a certain base is the s...