2013-05-24 09:06:20Morris

[UVA][math] 12546 - LCM Pair Sum

One of your friends desperately needs your help. He is working with a secret agency and doing some encoding stuffs. As the mission is confidential he does not tell you much about that, he just want you to help him with a special property of a number. This property can be expressed as a function f (n) for a positive integer n. It is defined as:

f (n) = $displaystyle sum_{{begin{array}{c}{1 le p le q le n} \  {lcm(p,q)=n}end{array}}}^{}$(p + q)

In other words, he needs the sum of all possible pairs whose least common multiple is n. (The least common multiple (LCM) of two numbers p and q is the lowest positive integer which can be perfectly divided by both p and q). For example, there are 5 different pairs having their LCM equal to 6 as (1, 6), (2, 6), (2, 3), (3, 6), (6, 6). So f (6) is calculated as f (6) = (1 + 6) + (2 + 6) + (2 + 3) + (3 + 6) + (6 + 6) = 7 + 8 + 5 + 9 + 12 = 41.

Your friend knows you are good at solving this kind of problems, so he asked you to lend a hand. He also does not want to disturb you much, so to assist you he has factorized the number. He thinks it may help you.

Input 

The first line of input will contain the number of test cases T (T$ le$500). After that there will be T test cases. Each of the test cases will start with a positive number C (C$ le$15) denoting the number of prime factors of n. Then there will be C lines each containing two numbers Pi and ai denoting the prime factor and its power (Pi is a prime between 2 and 1000) and ( 1$ le$ai$ le$50). All the primes for an input case will be distinct.

Output 

For each of the test cases produce one line of output denoting the case number and f (n) modulo 1000000007. See the output for sample input forexact formatting.

Sample Input 

3
2
2 1
3 1
2
2 2
3 1
1
5 1

Sample Output 

Case 1: 41
Case 2: 117
Case 3: 16


觀察一下,
單純每個 sigma 數字被計算到的次數。
分兩類:1) 不具有高次的數字 2) 具有高次的數字

1) 數字 number < N,只會被計算到一次,因為一定是 (number, N)

2) 數字 number 能搭配的次數,是具有高次的次方連乘積,不具有高次的則會出現在另一個數字身上。

最後答案是
sum = (1 + p1 + p1^2 + ... + p1^(num1-1) + (num1+1)*p1^num1)*

(1 + p2 + p2^2 + ... + p2^(num2-1) + (num2+1)*p2^num2)* ... *
(1 + pi + pi^2 + ... + pi^(numi-1) + (numi+1)*pi^numi) + N;


#include <stdio.h>
#define mod 1000000007LL
int main() {
int testcase;
int cases, n, i, j;
long long pi, ai, ans;
scanf("%d", &testcase);
cases = 0;
while(testcase--) {
scanf("%d", &n);
ans = 1;
long long N = 1;
for(i = 0; i < n; i++) {
scanf("%lld %lld", &pi, &ai);
long long tmp = 1, sum = 0;
for(j = 0; j < ai; j++) {
sum += tmp;
sum %= mod;
tmp = tmp*pi;
tmp %= mod;
}
N *= tmp;
N %= mod;
tmp = tmp*(ai+1);
tmp %= mod;
sum = (sum+tmp)%mod;
ans *= sum;
ans %= mod;
}
printf("Case %d: %lld\n", ++cases, (ans+N)%mod);
}
return 0;
}