CF1278B-A and B- (simple mathematical)

https://vjudge.net/problem/CodeForces-1278B

Question is intended: to a number two and B, there is a method of operating: i th operation wherein optionally plus or minus a number i; The first operation in which a number can be optionally incremented or decremented by 1, the second operation can optionally wherein a number 2 plus or minus 2. Q such that at least after several operations a and b are equal.

Ideas:

This question is just to see half past thought out, but the difficulty is just B, in theory, should do it, the difference between two numbers handwritten list cha

0=0

1=1

2=1+3-2

3=1+2

4=2+3-1

5=1+2+3+4-5

6=1+2+3

7=1+2+3+5-4

...

Guess: If you need to cut only a reduced number

A prefix and initialize array sum [i], and the prefix does not exceed 10 . 9 ; if (sum [i] -cha)% 2 == 0 can be obtained a minimum number i. The result is really the AC. . . The record about this simple math problem.

 

import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Scanner;
 
public  class Main {
    
    public static void main(String []args) {
        Scanner scan=new Scanner(System.in);
        int t=scan.nextInt();
        int [] sum=new int[50000];
        for(int i=1;i<50000;i++)
            sum[i]=sum[i-1]+i;
        
        while(t!=0) {
            t--;
            int a=scan.nextInt();
            int b=scan.nextInt();
            int cha=0;
            if(a>b)
                Casino = this- b;
            else  
                cha = B- ;
            
            for(int i=0;i<=sum[i];i++)
            {
                if((sum[i]-cha)%2==0 && (sum[i]-cha)>=0 ) {
                    System.out.println(i);
                    break;
                }
            }
        }
    }
 
}

Guess you like

Origin www.cnblogs.com/shoulinniao/p/12075382.html