You are given a positive integer and your task is to find the summation of its perfect square divisors.
Example:
Input: n=2, arr2D[][] = { { 2, 2 }, { 3, 1 } }
Output: 5
Approach
Java
public class PerfectDivisors {static long mod = (int) 1e9 + 7;static long power(long i, long p) {long res = 1;for (; p > 0; p = p >> 1) {if (p % 2 == 1)res = (res * i) % mod;i = (i * i) % mod;}return res;}public static void main(String[] args) throws Exception {int M = 2000000 + 1;// Sieveint prime[] = new int[M];for (int i = 2; i < M; i += 2) {prime[i] = 2;prime[i - 1] = i - 1;}for (int i = 3; i * i < M; i += 2)if (prime[i] == i)for (int j = i * i; j < M; j += i)if (prime[j] == j)prime[j] = i;int n = 2;int arr2D[][] = { { 2, 2 }, { 3, 1 } };long map[] = new long[M];for (int i = 0; i < n; i++) {int x = arr2D[i][0];int y = arr2D[i][1];while (x > 1) {map[prime[x]] += y;map[prime[x]] %= mod;x /= prime[x];}}long ans = 1;for (int i = 2; i < M; i++) {if (map[i] < 2)continue;long a = (1L * i * i) % mod;long temp = 0;if (map[i] == 2) {temp = a;} else {long pow = power(a, map[i] / 2);pow -= 1;if (pow < 0)pow += mod;long numo = (pow * a) % mod;long deno = power(a - 1, mod - 2) % mod;temp = (numo * deno) % mod;}temp = (temp * ans) % mod;ans += temp;if (ans >= mod)ans -= mod;}System.out.println(ans);}}
No comments:
Post a Comment