Hungry Xiaoyi (Law, Congruence Large Numbers)

Topic description

Xiaoyi is always hungry, so Xiaoyi, who is an octopus, often goes out to find shells to eat. At the beginning, Xiaoyi is at an initial position x_0. For Xiaoyi's current position x, he can only move to 4*x+3 or 8*x+7 through mystical power. Because it takes too much stamina to use the mysterious power, it can only use the mysterious power up to 100,000 times. Shells always grow at positions divisible by 1,000,000,007 (eg: position 0, position 1,000,000,007, position 2,000,000,014, etc.). Xiaoyi needs your help to calculate the minimum number of times the mysterious power needs to be used to eat the shell.

Enter description:

Enter an initial position x_0 in the range 1 to 1,000,000,006

Output description:

Output the minimum number of times that Xiaoyi needs to use the mysterious power. If the shell is not found after the number of uses, output -1
Example 1

enter

125000000

output

1
1  import java.util.Scanner;
 2  
3  /** 
4  * Hungry Xiaoyi 
 5  * 1, analysis
 6  * 4x+3 
 7  * 8x+7 
 8  * 4(4x+3)+3 = 16x+15
 9  * 4(8x+7)+3 = 8(4x+3)+7 =32x+31 =32(x+1)-1 
 10  * The number obtained by each transformation is 
 11  * We set 2^n as times, There are also times(x+1)-1, a=2^n * x +(2^n)-1
 12  * Loop to check whether a%N is 0, judge 
 13  * 2, use up to 100000 times of magic, in most cases, it is all Using 8x+7,
 14  * At this time we are using 2 to loop, 2^3 = 8 Then we need to loop 300000 times 
 15  * However, neither int nor long can represent such a large number, so we need to do Congruence processing to replace
 16  *  
17  * Congruence:
 18           If a%m = b%m then we say that ab is congruent modulo m, denoted as a≡b(mod m) 
 19           It is not difficult to understand that b = a%m has a≡b(mod m ) 
 20           Then: 
 21           Let the remainder of a and b for m be mod, which can be expressed as follows: 
 22          a = c1*m + mod 
 23          b = c2*m + mod (a1, a2 are different integers) 
 24          a(x +1)-1 = (c1*m + mod)*(x+1)-1 = c1*m*(x+1) + mod*(x+1)-1 
 25          b(x+1)-1 = (c2*m + mod)*(x+1)-1 = c2*m*(x+1) + mod*(x+1)-1 
 26          c1*m*(x+1), c2*m *(x+1) are all divisible by m by 
 27          , so the remainder of a(x+1)-1 b(x+1)-1 for m is (mod*(x+1)-1)%m 
 28           That is to say, a≡b(mod m), after ab does the same addition, subtraction, multiplication and division transformation, the congruence for m still holds 
 29          
30       Therefore, we can use times=times%N to find the congruence of times to N, before and after (times*(x+1)-1)%N value can be replaced by
 31  * 
 32  * 
 33  * 
 34  * 4x+3 equals twice 2x+1 8x+7 equals three times 2x+1
 35  * 
 36  * @author Dell
 37  *
 38   */ 
39  public  class Main {
 40      static  public  long x = 125000000 ;
 41      static  public  long N = 1000000007 ;
 42      static  public  long count = -1 ;
 43     static  public  long times = 4;   // The first minimum is 4 
44      static  public  void f() {
 45          for ( int i = 1; i <=300000; i++ ) {
 46              long mod = (times*(x+1 )-1)% N;
 47              if (mod == 0 ) {
 48                  // take as many as possible 8 to reduce the number of steps, so divide by 3
 49                  // times start from 4 and it is 2^2 and i from 1 must be added 1 at the beginning 
50                  count=(i+1)/3 + ((i+1)%3==0?0:1 );
 51                  return ;
 52              }
 53              // Find the next times and do the replacement
54             times = (times*2)%N;
55         }
56     }
57     public static void main(String[] args) {
58         Scanner sc = new Scanner(System.in);
59         x = sc.nextLong();
60         f();
61         System.out.println(count);
62     }
63 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325133110&siteId=291194637