Set bits in conjunction with exponential modular arithmetic

description of the problem

Yesterday I got this question in a task. I thought I encoded it correctly, and my test case was accepted. However, there was not even one test for the backend. Here is my code. Please someone help me. The problem is solved for me, and therefore I can not imagine it further. But I want to learn from my mistakes. Thank.

  import java.io.*;
//import java.util.*;


public class TestClass {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter wr = new PrintWriter(System.out);
         int n = Integer.parseInt(br.readLine().trim());
         String[] arr_a = br.readLine().split(" ");
         int[] a = new int[n];
         for(int i_a=0; i_a<arr_a.length; i_a++)
         {
            a[i_a] = Integer.parseInt(arr_a[i_a]);
         }

         long out_ = solve(a);
         System.out.println(out_);

         wr.close();
         br.close();
    }
    static long solve(int[] a){
        // Write your code here
        long sum = 0l;
        long MAX = 10000000011l;
        long i = 1l;
        for(int x : a) {
            long count = 0;
            while(x>0) {
                x &= (x-1l);
                count++;
            }
            long res = 1l;
            long temp = i;
            count = count % MAX;
            while(temp > 0) {
                if((temp & 1l) == 1l) {
                    res = (res * count) % MAX;
                }
                temp = temp >> 1l;
                count = ((count % MAX) * (count % MAX)) % MAX;

            }

            long t =((sum%MAX) + (res % MAX))%MAX;
            sum = t;
            i++;
        }

        return sum;
    }
}
+4
source share
1 answer

It’s a little strange that “not even one test case passed”, but the only mistake I see is the squaring of the squared part.

10^10 + 11, 32 , ( long - 64- ).

:

  • (a*b) % M , " ". . O(log(n)) " 2". :

    static long multiply(long a, long b, long M) {
        long res = 0;
        long d = a % M;
    
        while (b > 0) {
            if ((b & 1) == 1) {
                res = (res + d) % M;
            }
    
            b >>= 1;
            d = (d + d) % M;
        }
        return res;
    }
    
  • b^i % M . ( ) last(b) - i, a[i] b . last(b) + 1 i.

  • BigInteger .

+1

Source: https://habr.com/ru/post/1681503/


All Articles