https://www.luogu.org/problem/show?pid=3518 問題描述: 輸入格式:n,k k個非負整數 輸出格式:一個數 樣例:入 42 5 28 31 10 38 24 出 14 k<=250 000 ,k<=n<=10^14; 分析;當x為密碼時,x 的因數和倍數都是 ...
https://www.luogu.org/problem/show?pid=3518
問題描述:
輸入格式:n,k
k個非負整數
輸出格式:一個數
樣例:入 42 5
28 31 10 38 24
出 14
k<=250 000 ,k<=n<=10^14;
分析;當x為密碼時,x 的因數和倍數都是密碼。
所以只需找到最小的x,即可。
代碼目標:找出的gcd(a[k],n)所有因數記在q數組中。
從中刪去gcd(a[k],a[1~k-1])的因數。
q中剩下的最小因數,即為所求x。
輸出n/x 就OK了
#include<iostream> #include<cstring> #include<algorithm> #include<cstdio> #include<cmath> #include<queue> #include<vector> #include<climits> #include<string> #include<cstdlib> //#include<ctime> using namespace std; #define LL long long LL n,k,i,j; LL a[1000000],q[5000000],cnt=0,ans; bool f[1300000]; LL gcd(LL a,LL b) { LL t; while(b) { t=a; a=b;b=t%b; } return a; } int main() { scanf("%lld%lld",&n,&k); for(i=1;i<=k;i++) scanf("%lld",&a[i]); a[k]=gcd(a[k],n); for(LL i=1;i<k;i++) a[i]=gcd(a[i],a[k]); for(i=1;i*i<=a[k];i++) if(a[k]%i==0) { q[++cnt]=i; if(i*i!=a[k]) q[++cnt]=a[k]/i; } sort(q+1,q+cnt+1); for(LL i=1;i<k;i++) f[lower_bound(q+1,q+cnt+1,a[i])-q]=1;//莫意思 for(i=1;i<=cnt;i++) if(f[i]) for( j=1;j<i;j++) if(q[i]%q[j]==0) f[j]=1; for(ans=1;f[ans];ans++); printf("%lld",n/q[ans]); return 0; }