问题 B: 要塞任务(数论)

请小伙伴们对自己AC的题目进行标记,注意每人只能标记一次!不知道的不要标记,恶意标记者将回收账号!!!

问题 B: 要塞任务

时间限制: 1 Sec  内存限制: 128 MB
[提交] [状态]

题目描述

你的要塞⾥有N名随从,每名随从有⼀个战⽃⼒值Ai,不同随从的战⽃⼒可以相同,且永远不超过N。⼀个要塞任务需要恰好M个随从参与。
要塞任务的奖励取决于随从们配合的程度。(显⽽易见地),M个随从的联合战⽃⼒A为它们战⽃⼒的最⼤公约数,⽽任务的奖励分数定义为ϕ(A)。
求最⼤可能的奖励分数。

输入

本题有多组数据,第⼀⾏为数据组数T(T≤10)。
接下来每组数据有两⾏,第⼀⾏两个整数N,M,第⼆⾏N个整数Ai(N,M,Ai≤100000)。

输出

最多的奖励分数。

样例输入 Copy

1
5 2
1 4 6 9 12

样例输出 Copy

2

提示

样例解释:派出编号为6和12的随从,联合战⽃⼒为3,奖励分数2。
#include<bits/stdc++.h> 
#include <math.h>
using namespace std;
typedef long long ll; 
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
const int maxn=1e5+100;
const int inf=0x3fffffff;
const int mod=1e9+7; 
int biaoji[maxn],p[maxn],ol[maxn],cnt,a[maxn];
void P(){
    for(int i=2;i<=maxn;i++){
        if(!biaoji[i]){
            p[++cnt]=i;
            ol[i]=i-1;
        }
        for(int j=1;j<=cnt&&p[j]*i<=maxn;j++){
            biaoji[p[j]*i]=1;
            if(i%p[j]==0){
                break;
            }
        } 
    }
    ol[1]=1;
    int t;
    for(int i=2;i<=maxn;i++){
        if(biaoji[i]){
            ol[i]=i;
            t=i;
            for(int j=1;j<=cnt&&t>1;j++)
            {
                if(i%p[j]==0)
                {
                    ol[i]=ol[i]*(p[j]-1)/p[j];
                    while(t%p[j]==0){
                        t/=p[j];
                    }
                }
                if(p[j]*2>i){
                    break;
                }
            }
        }
    }
}
int t;
void inint(){
    cin>>t;
}
int main(){
    P();
    int ans=-1,n,m;
    int ma=-1e9,tot=0;
    inint();
    while(t--){
        ans=-1;
        ma=-1e9;
        memset(a,0,sizeof(a));
        cin>>n>>m;
        for(int i=1;i<=n;i++){
            int p;
            p=read();
            a[p]++;
            ma=max(ma,p);
        }
        for(int i=1;i<=ma;i++){
            tot=0;
            for(int j=i;j<=ma;j+=i){
                tot+=a[j];
            }
            if(tot>=m){
                ans=max(ans,ol[i]);
            }
        }
        printf("%d
",ans);
    }
}  
原文地址:https://www.cnblogs.com/lipu123/p/12824393.html