lightoj1370欧拉函数

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 nwhich 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

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

Output

For each case, print the case number and the minimum possible money spent for buying the bamboos. See the samples for details.

Sample Input

3

5

1 2 3 4 5

6

10 11 12 13 14 15

2

1 1

Sample Output

Case 1: 22 Xukha

Case 2: 88 Xukha

Case 3: 4 Xukha

bamboo的score值就是其长度x的欧拉函数值(即小于x且与x互质的数的个数)欧拉函数有一个性质:素数p的欧拉函数值为p-1;

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 int a[1000000+10];
 5 int t,n,x;
 6 long long sum;
 7 int b[1000000+10]={1,1,0};
 8 void f()
 9 {
10     for(int i=2;i<=1000000+9;i++)
11     {
12         if(!b[i])
13         {
14             for(int j=i+i;j<=1000000+9;j+=i)
15                 b[j]=1;
16         }
17     }
18 }
19 int main()
20 {
21     f();
22     while(scanf("%d",&t)!=EOF)
23     {
24         for(int w=1;w<=t;w++)
25         {
26             sum=0;
27             scanf("%d",&n);
28             for(int i=1;i<=n;i++)
29             {
30                 scanf("%d",&x);
31                 for(int k=x+1;k<=1000000+9;k++)
32                 {
33                     if(b[k]==0)
34                     {
35                         sum+=k;
36                         break;
37                     }
38                 }
40             }
41             printf("Case %d: %lld Xukha\n",w,sum);
42         }
43
44
45     }
46     return 0;
47 }
时间: 2024-10-07 05:31:23

lightoj1370欧拉函数的相关文章

lightOJ1370 欧拉函数性质

D - (例题)欧拉函数性质 Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Description Bamboo Pole-vault is a massively popular sport in Xzhiland. And Master Phi-shoe is a very popul

LightOJ1370 Bi-shoe and Phi-shoe 欧拉函数

LightOJ1370 Bi-shoe and Phi-shoe 标签 欧拉函数 前言 我的csdn和博客园是同步的,欢迎来访danzh-博客园~ 简明题意 给出一个序列a[],\(b_i\)=欧拉函数值>=\(a_i\)的最小i,现在求\(b_i\)的和. 思路 现在只考虑一个数a,求phi[i]>=a的最小i,显然是应该从1开始遍历phi[]数组,一旦找到一个i使得phi[i]>=a,那么i就是答案. 现在有很多数,如果一个个的找,复杂度太高.但是发现,对于a1>a,a1的答案

lightoj1370——Bi-shoe and Phi-shoe(欧拉函数应用)

Description 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

欧拉函数

void Euler_Sieve_Method(int * euler, int n) { euler[1] = 1; for (int i = 2; i < n; i++) { euler[i] = i; } for (int i = 2; i < n; i++) { if (euler[i] == i) { for (int j = i; j < n; j += i) { euler[j] = euler[j] / i * (i - 1); } } } } void Euler_Si

hdu1695(莫比乌斯)或欧拉函数+容斥

题意:求1-b和1-d之内各选一个数组成数对,问最大公约数为k的数对有多少个,数对是有序的.(b,d,k<=100000) 解法1: 这个可以简化成1-b/k 和1-d/k 的互质有序数对的个数.假设b=b/k,d=d/k,b<=d.欧拉函数可以算出1-b与1-b之内的互质对数,然后在b+1到d的数i,求每个i在1-b之间有多少互质的数.解法是容斥,getans函数参数的意义:1-tool中含有rem位置之后的i的质因子的数的个数. 在 for(int j=rem;j<=factor[i

欧拉函数常用性质

欧拉函数定义:设n 为正整数,则1,2......,n中与n互质的整数个数记作f(n). 1.1 若n为素数,f(n)=n-1; 1.2 整数n=p*q,p,q为不同素数,则f(n)=f(p)*f(q)=(p-1)*(q-1) 1.3 n=p^a*q^b,f(n)=f(p^a)*f(q^b)=n*(1-1/p)*(1-1/q) 1.4 分解质因子相乘,f(n)=n*(1-1/p1)*(1-1/p2)*.......*(1-1/pk). f(100)=f(2^2*5^2)=100*1/2*4/5=

POJ2478(SummerTrainingDay04-E 欧拉函数)

Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16927   Accepted: 6764 Description The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational numbers a/b with 0 < a < b <= n and gcd(a,b)

POJ 2478 欧拉函数(欧拉筛法) HDU 1576 逆元求法

相关逆元求法,我之前有写过,还有欧拉函数的求法,欧拉函数与逆元的关系  点击 POJ 2478 又是一个打表的题目,一眼看出结果就是前n个欧拉函数值的和. 这里直接计算欧拉函数值求和会超时,看见多组数据. 然后就是计算欧拉函数,打表就好了. #include <stdio.h> #include <string.h> #include <iostream> using namespace std; typedef long long LL; const int N =

算法复习——欧拉函数(poj3090)

题目: Description A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal to 0), other than the origin, is visible from the origin if the line from (0, 0) to (x, y) does not pass through any other lattice point. For exa