题目链接:http://lightoj.com/volume_showproblem.php?problem=1370
题目描述:
Bamboo Pole-vault is a massively popular sport in Xzhiland. And Master Phi-shoe is a very popular coach for his success. He needs some bamboos for his students, so he asked his assistant Bi-Shoe to go to the market and buy them. Plenty of Bamboos of all possible integer lengths (yes!) are available in the market. According to Xzhila tradition,
Score of a bamboo = Φ (bamboo’s length)
(Xzhilans are really fond of number theory). For your information, Φ (n) = numbers less than n which are relatively prime (having no common divisor other than 1) to n. So, score of a bamboo of length 9 is 6 as 1, 2, 4, 5, 7, 8 are relatively prime to 9.
The assistant Bi-shoe has to buy one bamboo for each student. As a twist, each pole-vault student of Phi-shoe has a lucky number. Bi-shoe wants to buy bamboos such that each of them gets a bamboo with a score greater than or equal to his/her lucky number. Bi-shoe wants to minimize the total amount of money spent for buying the bamboos. One unit of bamboo costs 1 Xukha. Help him.
输入:
Input starts with an integer T (≤ 100), denoting the number of test cases.
Each case starts with a line containing an integer n (1 ≤ n ≤ 10000) denoting the number of students of Phi-shoe. The next line contains n space separated integers denoting the lucky numbers for the students. Each lucky number will lie in the range [1, 106].
输出:
For each case, print the case number and the minimum possible money spent for buying the bamboos. See the samples for details.
题目大意: Φ (n)表示长度为小于数字n的和n互质的数的个数,也就是欧拉函数。现在给出n个幸运数字,对于每一个幸运数字,要求的x,使Φ (x)的值大于等于这个幸运数字,求这些x和的最小值。
解题思路:可以先把题目数据范围内的欧拉函数求解出来,欧拉函数的求法可以自己写。然后再对比输入的数据进行选择。
参考代码:
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
#define ll long long
#define clean(arrays) memset(arrays, 0, sizeof(arrays))
#define N 1000006
int prime[N], mark[N]; // prime 为素数数组, mark 标记是否为素数
int phi[N], indexs = 0; // phi[i] 为φ(i), indexs 为出现素数的个数
//欧拉函数线性筛
void line_phi(int n)
{
clean(phi); clean(prime);
phi[1] = 1; // φ(1) = 1;
for (int i = 2; i <= n; i++)
{
if (! mark[i]) { // 如果是素数(为0)
prime[++indexs] = i; //进素数数组,指针加 1
phi[i] = i - 1;
}
for (int j = 1; j <= indexs; j++)
{
if (i * prime[j] > n) break;
mark[i * prime[j]] = 1; // 标记 i * prime[j] 不是素数
if (i % prime[j] == 0) { //应用性质 1
phi[i * prime[j]] = phi[i] * prime[j];
break;
}
else {
phi[i * prime[j]] = phi[i] * (prime[j] - 1); //应用性质 3
}
}
}
}
int a[1000005];
int main()
{
line_phi(N); //先把欧拉函数求出来
int t, m;
cin >> t;
for (int i = 1; i <= t; i++)
{
cin >> m;
for (int j = 1; j <= m; j++)
cin >> a[j];
sort(a + 1, a + m + 1); // 对数组进行排序,避免以后的重复计算
int x = 2;
ll ans = 0; // 最后累加的和是个比较大的数
for (int j = 1; j <= m; j++)
{
while (phi[x] < a[j])
x++;
ans += x;
}
cout <<"Case " << i << ": " << ans << " " <<"Xukha"<<endl;
}
}