[Programming question] Divisible by 3

【question】:

Small Q gets a magical sequence: 1, 12, 123,...12345678910,1234567891011....

And small Q is very interested in the property of whether it is divisible by 3.

Xiao Q now hopes that you can help him calculate how many numbers from the lth to the rth (including the endpoints) of the sequence are divisible by 3.

Enter description:

The input consists of two integers l and r (1 <= l <= r <= 1e9), representing both ends of the interval to be solved.

Output description:

Output an integer representing the number of digits in the interval that are divisible by 3.

Input example 1:

2 5

Output example 1:

3

Example Description 1:

12, 123, 1234, 12345...
12, 123, 12345 are divisible by 3.

【solve】:

【law】

1. 1. The number whose single digit is an even number (0, 2, 4, 6, 8) can be divisible by 2;
2. 2. The number whose single digit is 0 or 5 is divisible by 5
; 3. The number whose last two digits are divisible by 4 (or 25) can be divisible by 4 (or 25)
; 4. The number whose last three digits are divisible by 8 (or 125) can be divisible by 8 (or 125)
; A number that is divisible by 6 only needs to be divisible by 2 and 3.
5. 6. The sum of the digits can be divisible by 3 (or 9) and can be divisible by 3 (or 9)
; 7. The difference between the sum of odd-digit numbers and the sum of even-digit numbers is divisible by 11 
; The difference between the number represented by the last three digits and the number represented by the digits before the last three digits (large number minus the number) The number that is divisible by 7 (or 11 or 13) is divisible by 7 (or 11 or 13)

[Note] When the number is greater than 10, each single digit needs to be added.

import java.util. *;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int l = sc.nextInt();
        int r = sc.nextInt();
        int sum = 0;
        for (int i = 1;i < l;i ++){
            int tmp = i;
            while(tmp != 0){
                sum += tmp % 10;
                tmp /= 10;
            }
        }
        int count = 0;
        for (int i = l;i <= r;i ++){
            int tmp = i;
            while(tmp != 0){
                sum += tmp % 10;
                tmp /= 10;
            }
            if (sum % 3 == 0){
                count ++;
            }
        }
        System.out.println(count);
    }
}

Timed out, too many loops! ! !

-------------------------------------------------------------------------------------------------------------

After tossing for a long time, I found that just change all types to long. . . .

import java.util. *;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long l = sc.nextInt();
        long r = sc.nextInt();

        long sum = 0;
        for (long i = 1;i < l;i ++){
            sum += i;
        }
        long count = 0;
        for (long i = l;i <= r;i ++){
            sum += i;
            if (sum % 3 == 0){
                count ++;
            }
        }
        System.out.println(count);
    }
}

-------------------------------------------------------------------------------------------------------------

 ② Find the rules:

When i is inserted, there are the following rules:

i = 1 ---- 1----> 1

i = 2 ---- 12----> 0

i = 3 ---- 123----> 0

i = 4 ---- 1234 ---->1

i = 5 ---- 12345 ---->0

i = 6 ---- 123456 ---->0

i = 7 ---- 1234567 ----> 1

It can be seen that there are a total of f(x) = (x+2)/3 1s between the interval [1,x] , and the rest meet the requirements

Then in the r-l+1 numbers on the interval [l, r], f(r) - f(l-1) numbers that do not meet the requirements must be cut out .

Just print it out directly, O(1), no need to loop.

import java.util. *;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int l = sc.nextInt();
        int r = sc.nextInt();
        int count = r - l + 1 - (f(r) - f(l - 1));
        System.out.println(count);
    }
    public static int f(int x){
        return (x + 2) / 3;
    }
}

Guess you like

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