uva12546. LCM Pair Sum

uva12546. 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

 

这道题目也也搞了很长时间,算是初识母函数吧,这道题目用到了这种思想。做完了,感觉还是不太明白怎么就能用

(1+a1+a1^2...(c1+1)*a1^c1)*(1+a2+a2^2...(c2+1)*a2^c2)*.....*(1+am+am^2...(cm+1)*am^cm)+n 这个公式。

 

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int MOD = 1000000007;

int main() {
      int TCase;
      cin >> TCase;
      for(int t = 1; t <= TCase; ++t) {
            long long n, p, c;
            long long ans = 1, flag = 1;
            cin >> n;
            for(int i = 0; i != n; ++i) {
                  long long tmp = 1, fac = 1;
                  cin >> p >> c;
                  for(int j = 0; j != c; ++j) {
                        fac = (fac * p) % MOD;
                        tmp = (tmp + fac) % MOD;
                  }
                  tmp = (tmp + (fac * c) % MOD) % MOD;
                  flag = (fac * flag) % MOD;
                  ans = (ans * tmp) % MOD;
            }
            ans = (ans + flag) % MOD;
            cout << "Case " << t << ": " << ans << endl;
      }
      return 0;
}

 

 

 

转载于:https://www.cnblogs.com/ya-cpp/p/4675395.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值