Advertisement
ArtemisL2

C2 in LOSH 2016

Jul 30th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.87 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3.  
  4.  
  5.  
  6. public class Main {
  7.     long mod = 1000*1000*1000+9;
  8.  
  9.     static long mu(long a, long b, long mod){
  10.         return (a*b)%mod;
  11.     }
  12.  
  13.     static long bin_pow(long a, long n, long mod){
  14.         if (n == 0) {
  15.             return 1;
  16.         }
  17.         if (n % 2 == 0) {
  18.             long g = bin_pow(a, n/2, mod);
  19.             return mu(g,g, mod);
  20.         } else {
  21.             return mu(a, bin_pow(a,n-1,mod),mod);
  22.         }
  23.     }
  24.    
  25.     public static void main(String[] args) {
  26.         Scanner sc = new Scanner(System.in);
  27.         long mod = 1000*1000*1000+9;
  28.         int n = sc.nextInt();
  29.         long y = mod-2;
  30.         int a[] = new int[n];
  31.         for (int i = 0; i < n; i++){
  32.             a[i] = sc.nextInt();
  33.         }
  34.         for (int i = 0; i <n; i++){
  35.             System.out.println(bin_pow(a[i],y, mod));
  36.         }
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement