LightOJ 1370 Bi-shoe and Phi-shoe(素数筛)

Click Here~~
A - Bi-shoe and Phi-shoe
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu

Submit

Status

Practice

LightOJ 1370

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

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

题目大意:
首先给出T组数据,给一些数ai(第 i 个数),ai这些数代表的是某个数欧拉函数的值,我们要求出数 pi 的欧拉函数值不小于ai。而我们要求的就是这些 pi 这些数字的和sum,而且我们想要sum最小,求出sum至少是多少。

解题思路:
要想找到最小的和 只要我们考虑的是每个pi最小 因为素数的欧拉函数值是素数-1,所以我们只需要找到比ai大的第一个素数就行啦,然后我们就想到素数筛啦 哈哈~~ 其实数据范围是1e6,并不是很大 所以可以直接存下
详见代码:

#include <iostream>
#include <cstring>
using namespace std;
typedef long long LL;
const int MAXN = 1e6+5;
int prime[MAXN];///存素数的
int cnt;///素数的个数
bool p[MAXN];///判断素数滴
void isprime()///素数筛
{
    cnt = 0;
    memset(p, true, sizeof(p));
    p[1] = false;
    for(LL i=2; i<MAXN; i++)
    {
        if(p[i])
        {
            prime[cnt++] = i;
            for(LL j=i*i; j<MAXN; j+=i)
                p[j] = false;
        }
    }
}
///没有用的欧拉函数 白写了~~
///int Eular(int m)
///{
///    int ret = m;
///    for(int i=2; i<m; i++)
///    {
///        if(m%i == 0)
///        {
///            ret -= ret/i;
///            while(m%i)
///                m /= i;
///        }
///    }
///    if(m > 1)
///        ret -= ret/m;
///    return ret;
///}
int main()
{
    isprime();
    ///cout<<prime[cnt-1]<<endl;
    LL T, m, num;
    cin>>T;
    for(int cas=1; cas<=T; cas++)
    {
        cin>>m;
        LL sum = 0;
        while(m--)
        {
            cin>>num;
            for(int i=num+1; ; i++)///找到第一个素数
            {
                if(p[i])
                {
                    sum += i;
                    break;
                }
            }
        }
        cout<<"Case "<<cas<<": "<<sum<<" Xukha\n";
    }
    return 0;
}
时间: 2024-08-18 06:50:52

LightOJ 1370 Bi-shoe and Phi-shoe(素数筛)的相关文章

poj 3006 Dirichlet&amp;#39;s Theorem on Arithmetic Progressions 【素数筛】

说实话,题目很长,但是和真正要思考的东西关系不大... 就用了之前的素数筛的模板,控制了一下输入.输出格式就过了,很水的题,没什么技术含量,我好像也只会用暴搜... #include <stdio.h> #define MAXN 1000002 int prime[MAXN]; //用筛法求素数,1代表不是素数(被筛掉) int main() { //先打出素数表 prime[0]=prime[1]=1; //开始去掉prime[0]和prime[1] int i,j; for(i=2;i&l

UVa 640 Self Numbers:类似素数筛

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=581 关键: 01.if (!vis[i]) printf("%d\n", i); 02.vis[d(i)] = true; 完整代码: 01.#include<cstdio> 02.const int maxn = 100000

poj 2739 Sum of Consecutive Prime Numbers【素数筛】

我觉得这题的数据应该有些刁钻,一共至少提交了五次,一直是WA,无语了......用一个result数组存素数硬是不对.后来就算了,改用直接判断(法二),继续WA,后来发现是MAXN至少应为10002(被数据坑了),终于A掉了...... 后来继续改法一多次,任然WA,一直不清楚原因. 继续思考发现有一个很隐蔽的问题就是我后来用到   if(result[i]>n) break;    而result数组中 10000以内 最后一个素数是 9997,后面全是0了.这样无法停止,所以必须加一个大数作

poj 2262 Goldbach&amp;#39;s Conjecture 【素数筛】

这题必然的筛选法,出现了2个问题:1.开始开了一个 result 数组(全局变量),想把素数挨个存进来,虽然还计数估算了的,一直的runtime error , 后来发现是多此一举,去掉之后就变成wrong answer,看来可以编译了.这么说来,一百万对于两个大数组还是有点吃不消的  2.筛的时候一定要筛完整,for(j=2*i;j<MAXN;j+=i)prime[j]=1;这里开始用的 j<(MAXN/i),明显就错了. 另外我很好奇题目中说的"Goldbach's conjec

素数筛【模板】

#include <iostream> #define MAXN 1<<15 using namespace std; int prime[MAXN]; //为0代表是素数 int findPrime() { //先打出素数表 prime[0]=prime[1]=1; int i,j; for(i=2;i<MAXN;i++) { if(prime[i]==0) { for(j=2*i;j<MAXN;j+=i) prime[j]=1; } } return 0; }

UVa 10236 The Fibonacci Primes:斐波那契素数

10236 - The Fibonacci Primes Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1177 注意此题的描述和维基百科上Fibonacci Prime的描述不同. 上面加着重符的文字说明:可以用类似素数筛的方式筛出Fibonacci Pr

素数筛选法的进一步升级

今天晚上,正在翻书的时候,想学习一下数论,结果看到了素数筛的一部分,浴室我就温习了一下素数筛选法的代码, 我突然发现在筛素数的时候可以把外层循环缩小到他的根号2倍,嘿嘿,有点高兴啊... 上代码:(其实跟以前的差不多就是一样的) /** 2015 - 09 - 25 Author: ITAK Motto: 今日的我要超越昨日的我,明日的我要胜过今日的我, 以创作出更好的代码为目标,不断地超越自己. **/ #include <iostream> #include <cstdio>

E - Help Hanzo(LightOJ 1197)

传送门 Pssword: nefu Description Amakusa, the evil spiritual leader has captured the beautiful princess Nakururu. The reason behind this is he had a little problem with Hanzo Hattori, the best ninja and the love of Nakururu. After hearing the news Hanzo

《数学与泛型编程:高效编程的奥秘》一3.2 筛选素数

3.2 筛选素数 毕达哥拉斯学派的人还观察到一个现象,那就是有些数字没有办法表示成非平凡的矩形形状(nontrivial rectangular shape),也就是说,没有办法用两个边都大于1的矩形来表示.这种数叫做素数(prime number),它们不能够用比其更小且大于1的数之间的乘积来表示:2, 3, 5, 7, 11, 13,-(古希腊人所说的数都是指整数.)某些与素数有关的特征最早是由欧几里得观察到的.尽管大家通常都会把他与几何学联系起来,但是<几何原本>中有很多卷内容其实是在讲