Time Limit: 2 second(s) | Memory Limit: 32 MB |
Bamboo Pole-vault is a massively popular sport in Xzhiland.And Master Phi-shoe is a very popular coach for his success. He needs somebamboos for his students, so he asked his assistant Bi-Shoe to go to the marketand buy them. Plenty of Bamboos of all possible integer lengths (yes!) areavailable in the market. According to Xzhila tradition,
Score of a bamboo = Φ (bamboo's length)
(Xzhilans are really fond of number theory). For yourinformation, Φ (n) = numbers less thann which arerelatively prime (having no common divisor other than 1) ton. So, scoreof 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 eachstudent. 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 scoregreater than or equal to his/her lucky number. Bi-shoe wants to minimize thetotal amount of money spent for buying the bamboos. One unit of bamboo costs 1Xukha. Help him.
Input
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 linecontainsn space separated integers denoting the lucky numbers for thestudents. Each lucky number will lie in the range[1, 106].
Output
For each case, print the case number and the minimumpossible money spent for buying the bamboos. See the samples for details.
Sample Input |
Output for Sample Input |
3 5 1 2 3 4 5 6 10 11 12 13 14 15 2 1 1 |
Case 1: 22 Xukha Case 2: 88 Xukha Case 3: 4 Xukha |
题意是知道一串数字。然后规定一个规则,score 为 length以内与length互质的数的个数,每个数字依次分析,对于每个数字,求不小于这个数的score对应的length,然后加起来。
这个题可以转化一下题意,我们知道对于一个length为质数的数,他的score一定等于length - 1,然后对于任意一个合数,他的score一定小于它前面的那个质数的score,所以题意就转化为求解比当前数字大的最小的质数的大小,将所有的累计,得到最后的答案。
欧拉筛法解决。
代码如下:
/************************************************************************* > File Name: Bi-shoe_and_Phi-shoe.cpp > Author: Zhanghaoran > Mail: chilumanxi@xiyoulinux.org > Created Time: Tue 08 Dec 2015 12:38:03 PM CST ************************************************************************/ #include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <cstdlib> using namespace std; const int Max = 1000100; int Prim[Max]; int Isprim[Max]; void euler(){ int ans = 0; Isprim[0] = Isprim[1] = 1; for(int i = 2; i <= Max - 1; i ++){ if(!Isprim[i]) Prim[ans ++] = i; for(int j = 0; j < ans && Prim[j] * i <= Max - 1; j ++){ Isprim[Prim[j] * i] = 1; if(i % Prim[j] == 0) break; } } } int T; int N; int main(void){ cin >> T; long sum = 0; euler(); int cas = 0; while(T --){ cin >> N; cas ++; int temp; for(int i = 0; i < N; i ++){ cin >> temp; for(int j = temp + 1; j <= Max - 1; j ++){ if(!Isprim[j]){ sum += j; break; } } } printf("Case %d: %ld Xukha ", cas, sum); sum = 0; } }