【LightOJ - 1370】Bi-shoe and Phi-shoe(歐拉函數)


Bi-shoe and Phi-shoe

Descriptions:

給出一些數字,對於每個數字找到一個歐拉函數值大於等於這個數的數,求找到的所有數的最小和。

Input

輸入以整數T(≤100)開始,表示測試用例的數量。

每個案例都以一行包含整數n(1≤n≤10000)開始,表示有n個數。下一行包含n個空格分隔的整數,表示數字。每個數字都在[1,10^6]范圍內。

Output

求找到的所有數的最小和

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

題目鏈接

https://vjudge.net/problem/LightOJ-1370

 

先歐拉函數打表,再一一對照着找表即可

 

AC代碼

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#define Mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define MEM(x,y) memset(x,y,sizeof(x))
#define Maxn 1100000
using namespace std;
int T,n;
int phi[Maxn+1];//存歐拉函數
bool isPrime[Maxn+1];//存素數
int a[Maxn];
void Eular()//求歐拉函數
{
    for(int i=1;i<=Maxn;i++) phi[i]=i;
    memset(isPrime,true,sizeof(isPrime));
    isPrime[0]=isPrime[1]=false;
    phi[1]=0;
    for(int i=2;i<=Maxn;i++)
    {
        if(isPrime[i])
        {
            for(int j=i;j<=Maxn;j+=i)
            {
                isPrime[j]=false;
                phi[j] -= phi[j]/i;
            }
        }
    }
}
int main()
{
    int Case=0;
    Eular();//打表
    cin>>T;
    while(T--)
    {
        MEM(a,0);
        cin>>n;
        for(int i=0;i<n;i++)
            cin>>a[i];
        ll sum=0;
        sort(a,a+n);
        int pos=1;
        for(int i=0;i<n;i++)
        {
            for(int j=pos;j<Maxn;j++)
            {
                if(phi[j]>=a[i])
                {
                    sum+=j;
                    pos=j;
                    break;
                }
            }
        }
        printf("Case %d: %lld Xukha\n",++Case,sum);
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM