Group a analysis of the 6th Blue Bridge Cup (2015)

Posted by roopurt18 on Tue, 04 Jan 2022 11:22:15 +0100

Group a analysis of the 6th Blue Bridge Cup (2015)

  • 1 - bear monster eats walnuts
  • There is a bear monster in the forest. He likes walnuts very much, but he has the habit of dividing the walnuts he finds into two equal parts each time
  • Eat one and keep one. If it can't be divided equally, the bear monster will throw away a walnut and divide it again. The next day, the process will continue,
  • Until the last walnut is left, throw it away directly.
  • One day, the bear monster found 1543 brother walnuts. How many walnuts should he lose in the process of eating walnuts.
/**
 * 1-The bear monster eats walnuts
 * There is a bear monster in the forest. He likes walnuts very much, but he has the habit of dividing the walnuts he finds into two equal parts each time
 * Eat one and keep one. If it can't be divided equally, the bear monster will throw away a walnut and divide it again. The next day, the process will continue,
 * Until the last walnut is left, throw it away directly.
 * One day, the bear monster found 1543 brother walnuts. How many walnuts should he lose in the process of eating walnuts.
 */
public class Main {
    public static void main(String[] args) {
        int num =  1543;
        int count = 1 ; //Just throw the last one away
        while(num > 1){
           if(num > 1){
               if(num % 2 ==1) {
                   count++;
               }
               num /= 2 ;
           }else{
               break ;
           }
        }
        System.out.println(count);
    }
}

  • 2. Galaxy bomb
  • In the vast space of Galaxy X, there are many man-made bombs of star X floating as the road sign of the universe. Each bomb can be set to explode many days later
  • For example, if the alpha bomb was placed on January 1, 2015 and timed for 15 days, it will explode on January 16, 2015
  • There is a beta bomb, which was placed on November 9, 2014. The timing is 1000 days. Please calculate the exact date of its explosion
  • Please fill in the date in the format of yyyy MM DD, i.e. 4-digit year, 2-digit month and 2-digit date. For example, please write in strict accordance with the format on February 19, 2015. No other words or symbols can appear.
/**
 * 2.Galaxy bomb
 * In the vast space of Galaxy X, there are many man-made bombs of star X floating as the road sign of the universe. Each bomb can be set to explode many days later
 * For example, if the alpha bomb was placed on January 1, 2015 and timed for 15 days, it will explode on January 16, 2015
 * There is a beta bomb, which was placed on November 9, 2014. The timing is 1000 days. Please calculate the exact date of its explosion
 *
 * Please fill in the date in the format of yyyy MM DD, i.e. 4-digit year, 2-digit month and 2-digit date. For example, please write in strict accordance with the format on February 19, 2015. No other words or symbols can appear.
 *
 */
public class Main {
    public static void main(String[] args) {
        int t = 1000 - 365 - 366 - 21 - 31;
        int month = 1;
        while(t > 0){
            switch (month){
                case 1: case 3: case 5: case 7: case 8:case 10: case 12: t -= 31; month ++ ; break;
                case 2: t -= 28; month++; break;
                case 4: case 6: case 9: case 11: t-= 30; month++; break;
            }
        }

        System.out.println("2017-"+ "0" + (month-1) + "-" + "0" + (t+31) );

    }
}

  • 3. Nine numbers are divided into three groups. Numbers from 1 to 9 can form three three digits, which are set as a, B and C
  • The following relationships are now required:
  • B = 2 * A
  • C = 3 * A
  • Please write down all possible answers to A. the numbers are arranged in space and ascending order
import java.util.Arrays;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

/**
 * 3.Nine numbers are divided into three groups. Numbers from 1 to 9 can form three three digits, which are set as a, B and C
 * The following relationships are now required:
 * B = 2 * A
 * C = 3 * A
 * Please write down all possible answers to A. the numbers are arranged in space and ascending order
 */
public class Main {
    public static boolean equal(int a, int b, int c){
        int x1 = a / 100;
        int y1 = (a / 10) % 10 ;
        int z1 = a % 10 ;
        int x2 = b / 100;
        int y2 = (b / 10) % 10 ;
        int z2 = b % 10 ;
        int x3 = c / 100;
        int y3 = (c / 10) % 10 ;
        int z3 = c % 10 ;
        int [] res = {x1,x2,x3,y1,y2,y3,z1,z2,z3} ;

        Arrays.sort(res);
        String s = "" ;
        for(int i=0; i<res.length; i++){
            s += res[i] ;
        }
        if(s.equals("123456789")){
            return true ;
        }else{
            return false ;
        }
    }
    public static void main(String[] args) {
        Set<Integer> set = new TreeSet<>() ;
        for(int i=100; i<=999; i++){
            for( int j=100; j<=999; j++){
                for(int k=100; k<=999; k++){
                        if((j == 2*i) && (k == 3*i)){
                            if(equal(i,j,k)) {
                                set.add(i);
                            }
                        }
                }
            }
        }
        Iterator<Integer> iterator = set.iterator() ;
        while(iterator.hasNext()){
            System.out.print(iterator.next() + " ");
        }
    }
}

  • 4-cycle section length
  • When dividing two integers, sometimes a circular decimal will be generated, and its circular part is called a circular section,
  • The following method can calculate the length of the loop section, read the code carefully and fill in the code in the blank
import java.util.Vector;

/**
 * 4-Cycle section length
 * When dividing two integers, sometimes a circular decimal will be generated, and its circular part is called a circular section,
 * The following method can calculate the length of the loop section, read the code carefully and fill in the code in the blank
 */
public class Main {
    public static int f(int n, int m){
        n = n % m ;
        Vector v = new Vector() ;
        for(;;){
            v.add(n) ;
            n *= 10 ;
            n = n % m ;
            if(n == 0){
                return 0 ;
            }
            if(v.indexOf(n) >= 0){
                return v.size() - v.indexOf(n) ;
            }
        }
    }
    public static void main(String[] args) {
        System.out.println(f(11,13));

    }
}

5. Print Diamond: omitted

  • 6. Addition and multiplication
  • We all know that 1 + 2 + 3 +... + 49 = 1225
  • We now require that the two nonadjacent plus signs be changed into a multiplication sign so that the result is equal to 2015
  • Look for another answer and submit the number to the left of the multiplier.
/**
 * 6.Addition multiplication
 * We all know 1 + 2 + 3 ++ 49=1225
 * We now require that the two nonadjacent plus signs be changed into a multiplication sign so that the result is equal to 2015
 * Look for another answer and submit the number to the left of the multiplier.
 */
public class Main {
    public static void main(String[] args) {
        for(int i=1; i<=46; i++){
            for(int j=i+2; j<=48; j++){
                if(i*(i+1)+j*(j+1)-(i+i+1)-(j+j+1) == 2015-1225){
                    System.out.println(i + " " + j);
                }
            }
        }
    }
}

  • 7. Number of brand types
  • Xiao Ming was kidnapped to X casino and forced to play cards with the other three,
  • A deck of playing cards, excluding the big and small kings, a total of 52 cards, evenly distributed to 4 people, 13 for each person,
  • At this time, a problem suddenly appeared in Xiaoming's mind: if you don't consider the design and color, only consider the points, and don't consider the order in which you get the cards,
  • How many kinds of initial card combinations can you get in your hand?
/**
 * 7.Number of card types
 * Xiao Ming was kidnapped to X casino and forced to play cards with the other three,
 * A deck of playing cards, excluding the big and small kings, a total of 52 cards, evenly distributed to 4 people, 13 for each person,
 * At this time, a problem suddenly appeared in Xiaoming's mind: if you don't consider the design and color, only consider the points, and don't consider the order in which you get the cards,
 * How many kinds of initial card combinations can you get in your hand?
 */
public class Main {
    static int count = 0;

    /**
     *
     * @param k Number of card types, 1-13
     * @param cnt How many cards did you draw
     */
    public static void f(int k, int cnt){
        if(k>13 || cnt>13){
            return ;
        }
        if(k==13 && cnt==13){
            count ++ ;
        }
        for(int i=0; i<5; i++){ //The number of occurrences of each card type is 0-4
            f(k+1, cnt+i) ;
        }
    }
    public static void main(String[] args) {
         f(0, 0) ;
        System.out.println(count);
    }
}

  • 8. Moving distance
  • The residential buildings of Planet X are all the same, and arranged according to the matrix style. The building numbers are 1, 2, 3
  • When a row is full, row in the opposite direction from the adjacent building in the next row.
  • We know the two building numbers m and n, and we need to find the shortest moving distance between them. We can't move along the oblique line,
  • The input is 3 integers, w,m,n, separated by spaces, all in the range of 1-10000,
  • w is the row number width, m and N are the building number to be calculated. It is required to output a number to represent the shortest moving distance between M and n.
import java.util.Scanner;

/**
 * 8.Moving distance
 * X The residential buildings of the planet are all the same, and arranged according to the matrix style. The building numbers are 1, 2, 3
 * When a row is full, row in the opposite direction from the adjacent building in the next row.
 * We know the two building numbers m and n, and we need to find the shortest moving distance between them. We can't move along the oblique line,
 * The input is 3 integers, w,m,n, separated by spaces, all in the range of 1-10000,
 * w Is the row number width, m and N are the building number to be calculated. It is required to output a number to represent the shortest moving distance between M and n.
 */
public class Main {
    /**There is no test algorithm for the topic itself, but it is more logical. Of course, it is the sum of the absolute value of the row and column corresponding subtraction
     * The main difficulty is to find the corresponding number of columns according to the number of rows.
     */
 public static void main(String[] args) {
 Scanner input = new Scanner(System.in);
 int w = input.nextInt();
 int m = input.nextInt();
 int n = input.nextInt();
 int rm = m % w == 0 ? m / w  : m / w + 1;
 int rn = n % w == 0 ? n / w  : n / w + 1;
 int cm, cn;
 if (rm % 2 == 0) {
 cm = rm * w - m + 1;
 } else {
 cm = w - (rm * w - m);
 }
 if(rn % 2 == 0){
 cn = rn * w - n + 1 ;
 }else{
 cn = w - (rn * w - n) ;
 }
 System.out.println(Math.abs((rm-rn))+ Math.abs(cm-cn));
 }
 }

  • 9. Sieve (recursive method)
  • In his later years, gambler atm was infatuated with the dice on base, that is, to base the sieve one on top of the other, not crooked, but into a square column
  • After long-term observation, atm has discovered the mystery of stabilizing the sieve. Some numbers will be mutually exclusive if they fit together,
  • Let's standardize the screening Dice: the opposite of 1 is 4; Opposite 2 is 5; Opposite 3 is 6
  • Suppose that there are m groups of mutually exclusive phenomena, and the two number planes in each group are close together, the dice cannot be stably built
  • atm wants to calculate how many different possible base dice there are
  • The two base dice have the same way, if and only if the orientation of the corresponding number of the dice corresponding to the height is the same in both ways,
import java.util.Scanner;

/**
 * 9.Sieve
 * In his later years, gambler atm was infatuated with the dice on base, that is, to base the sieve one on top of the other, not crooked, but into a square column
 * After long-term observation, atm has discovered the mystery of stabilizing the sieve. Some numbers will be mutually exclusive if they fit together,
 * Let's standardize the screening Dice: the opposite of 1 is 4; Opposite 2 is 5; Opposite 3 is 6
 * Suppose that there are m groups of mutually exclusive phenomena, and the two number planes in each group are close together, the dice cannot be stably built
 * atm I want to calculate how many different possible ways to base dice
 * The two base dice have the same way, if and only if the orientation of the corresponding number of the dice corresponding to the height is the same in both ways,
 *
 */
public class Main {
    static int [] op = new int [7] ;
    static long count = 0 ;
    static int mod = 1000000007 ;
    static boolean [][] conflict = new boolean [7][7] ;
    public static void init(){ //Initialize opposite
        op[1] = 4;
        op[2] = 5;
        op[3] = 6;
        op[4] = 1;
        op[5] = 2;
        op[6] = 3;
    }

    /**
     * When the upper layer determines that the dice are facing up and the number is up, base the number of cnt dice
     * @param up above
     * @param cnt There are cnt more dice to combine
     * @return
     */
    public static long f(int up, int cnt){
        long ans = 0 ;
        if(cnt == 0){ //n dice have been positioned, recursive exit
            return 4 ;
        }
        for(int up1=1; up1<=6; up1++){//Lower recursion
            if(conflict[op[up]][up1]){ //Conflict cannot be selected as above
                continue;
            }
            ans = (ans + f(up1, cnt-1)) % mod ;
        }
        return ans ;
    }
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in) ;
        init() ;
        int n = input.nextInt() ; //Dice number
        int m = input.nextInt() ; //Number of rows
        for(int i=0; i<m; i++){
            int a = input.nextInt() ;
            int b = input.nextInt() ;
            conflict[op[a]][b] = true ;
            conflict[op[b]][a] = true ;
        }
        for(int up=1; up<=6; up++){
            count = (count +  4 * f(up, n-1)) % mod ;
        }
        System.out.println(count);
    }
}


9. Sieve (dynamic programming method)

import java.util.Scanner;

public class Main1 {
    static long mod = 1000000007;
    static int [] op = new int [7] ;
    static long [][] dp = new long [2][7] ; //Number of legal schemes with layer i j upward
    static void init(){
        op[1] = 4 ;
        op[2] = 5 ;
        op[3] = 6 ;
        op[4] = 1 ;
        op[5] = 2 ;
        op[6] = 3 ;
    }
   static  boolean [][] conflict = new boolean[7][7] ;
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in) ;
        init() ;
        int n = input.nextInt() ; //Dice number
        int m = input.nextInt() ; //Number of rows
        for(int i=0; i<m; i++){
            int a = input.nextInt() ;
            int b = input.nextInt() ;
            conflict[op[a]][b] = true ;
            conflict[op[b]][a] = true ;
        }
        for(int j=1; j<=6; j++){
            dp[0][j] = 1 ;
        }
        int cur = 0 ;
        for(int level=2; level<=n; level++){ //Number of iterations
            cur = 1 - cur ; //0-1 scrolling array
            //Participants placed six faces in the upward direction of the current layer
            for(int j=1; j<=6; j++){
                dp[cur][j] = 0 ;
                for(int i=1; i<=6; i++){
                    if(conflict[i][op[j]]){
                        continue;
                    }
                    dp[cur][j] = (dp[cur][j] + dp[1-cur][i]) % mod ; //Bottom up process
                }
            }
        }
        long sum = 0 ;
        for(int k=1; k<=6; k++){
            sum = (sum + dp[cur][k]) % mod ;
        }
        long ans = 1;
        long temp = 4;
        long p = n ;
        while(p != 0){ //Fast power solution to the nth power of 4
            if((p&1) == 1){
                ans = (ans * temp) ;
            }
            temp = (temp*temp) % mod ;
            p >>= 1 ;

        }
        System.out.println(sum * ans);
    }
}

10. Post disaster reconstruction (omitted)

Topics: Java Algorithm