首页 > 其他 > 详细

BZOJ2277 [Poi2011]Strongbox 【数论】

时间:2018-05-31 15:54:04      阅读:283      评论:0      收藏:0      [点我收藏+]

题目链接

BZOJ2277

题解

orz太难了

如果一个数\(x\)是密码,那么所有\((x,n)\)的倍数都是密码
如果两个数\(x,y\)是密码,那么所有\((x,y)\)的倍数都是密码

那么如果最后的密码集合为\(\{x_i\}\)那么一定存在一个\(x_i\)是剩余所有数的\(gcd\)
所以我们只需找最小的\(x | n\)\(x | a_k\)\(x \nmid a_i\)

那就找出\((a_k,n)\)的所有质因子,再用\((a_i,a_k,n)\)筛去不合法的即可

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 1000005,maxm = 10000005,INF = 1000000000;
inline LL read(){
    LL out = 0,flag = 1; char c = getchar();
    while (c < 48 || c > 57){if (c == ‘-‘) flag = -1; c = getchar();}
    while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
    return out * flag;
}
LL n,k,a[maxn],L,fac[maxn],p[maxn],tot,cnt;
int vis[maxn];
LL gcd(LL a,LL b){return b ? gcd(b,a % b) : a;}
void getfac(){
    LL x,tmp = L;
    for (x = 1; x * x <= tmp; x++){
        if (tmp % x == 0){
            fac[++tot] = x;
            if (x * x != tmp) fac[++tot] = tmp / x;
        }
    }
    sort(fac + 1,fac + 1 + tot);
}
void getp(){
    LL x,tmp = L;
    for (x = 2; x * x <=tmp; x++)
        if (tmp % x == 0){
            p[++cnt] = x;
            while (tmp % x == 0) tmp /= x;
        }
    if (tmp - 1) p[++cnt] = tmp;
}
int main(){
    n = read(); k = read();
    REP(i,k) a[i] = read();
    L = gcd(n,a[k]);
    getfac(); getp();
    LL x;
    for (int i = 1; i < k; i++){
        x = gcd(a[i],L);
        vis[lower_bound(fac + 1,fac + 1 + tot,x) - fac] = true;
    }
    for (int i = tot - 1; i; i--){
        if (vis[i]) continue;
        x = fac[i];
        for (int j = 1; j <= cnt && x * p[j] <= L; j++){
            LL y = x * p[j];
            int pos = lower_bound(fac + 1,fac + 1 + tot,y) - fac;
            if (fac[pos] == y && vis[pos]){
                vis[i] = true;
                break;
            }
        }
    }
    LL ans = 0;
    for (int i = 1; i <= tot; i++)
        if (!vis[i]){ans = n / fac[i]; break;}
    printf("%lld\n",ans);
    return 0;
}

BZOJ2277 [Poi2011]Strongbox 【数论】

原文:https://www.cnblogs.com/Mychael/p/9117144.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!