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) = (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 (T500). After that there will be T test cases. Each of the test cases will start with a positive number C (C15) 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 ( 1ai50). 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;
}
时间: 2024-10-11 11:31:20

uva12546. LCM Pair Sum的相关文章

[GeeksForGeeks] Find the largest pair sum in an unsorted array

Given an unsorted array, find the largest pair sum. Solution 1. O(n*logn) runtime, using sorting Solution 2. O(n) runtime, using heapify (max priority queue) Option 1. Use Java priority queue API, easy to implement but uses O(n) extra memory. Option

Subarray Sum Closest

Question Given an integer array, find a subarray with sum closest to zero. Return the indexes of the first number and last number. Given [-3, 1, 1, -3, 5], return [0, 2], [1, 3],[1, 1], [2, 2] or [0, 4]. Answer 这道题延续Subarray Sum的思路,即将[0, i]的sum存起来.这里

Two Sum III - Data structure design(leetcode170)

Design and implement a TwoSum class. It should support the following operations: add and find. add - Add the number to an internal data structure.find - Find if there exists any pair of numbers which sum is equal to the value. For example, add(1); ad

LeetCode 1099. Two Sum Less Than K

原题链接在这里:https://leetcode.com/problems/two-sum-less-than-k/ 题目: Given an array A of integers and integer K, return the maximum S such that there exists i < j with A[i] + A[j] = S and S < K. If no i, jexist satisfying this equation, return -1. Example

[LC] 1099. Two Sum Less Than K

Given an array A of integers and integer K, return the maximum S such that there exists i < j with A[i] + A[j] = S and S < K. If no i, j exist satisfying this equation, return -1. Example 1: Input: A = [34,23,1,24,75,33,54,8], K = 60 Output: 58 Expl

POJ 2773 Happy 2006 二分+容斥(入门

题目链接:点击打开链接 题意: 输入n ,k 求与n互质的第k个数(这个数可能>n) 思路: solve(mid)表示[1,mid]中有多少个和n互质,然后二分一下最小的mid 使得互质个数==k solve(x) 实现: 与n互质的个数=所有数-与n不互质的数=所有数-(与n有一个因子-与n有2个因子的+与n有3个因子的) 状压n的因子个数,然后根据上面的公式容斥得到. #include <stdio.h> #include <iostream> #include <

HDU 4135 Co-prime 区间内与n互质的个数 容斥(入门

题目链接:点击打开链接 题意:给定区间[l, r] 询问区间内有多少个数和n互质 思路: solve(x) 表示[1,x]区间内与n互质的个数,则ans = solve(r)-solve(l-1); 与n互质的个数=所有数-与n不互质的数=所有数-(与n有一个因子-与n有2个因子的+与n有3个因子的) 状压n的因子个数,然后根据上面的公式容斥得到. #include <stdio.h> #include <iostream> #include <algorithm> #

数位DP小结

如果以下错的地方,谢谢提出. 1.HDU - 2089 不要62 解题思路:这题的限制条件是不能出现4,和数字中不能包含62,那么就对这两个进行特判即可 #include<cstdio> #include<algorithm> #include<cstring> using namespace std; #define N 12 int n, m; int dp[N][N]; int data[N]; //zero_flag是前导零的标记,为0时表示有前导0 //bor

Java中的泛型 (上) - 基本概念和原理

本节我们主要来介绍泛型的基本概念和原理 后续章节我们会介绍各种容器类,容器类可以说是日常程序开发中天天用到的,没有容器类,难以想象能开发什么真正有用的程序.而容器类是基于泛型的,不理解泛型,我们就难以深刻理解容器类.那,泛型到底是什么呢? 什么是泛型? 一个简单泛型类 我们通过一个简单的例子来说明泛型类的基本概念.实现原理和好处. 基本概念 我们直接来看代码: public class Pair<T> { T first; T second; public Pair(T first, T se