[POI2011] SEJ-Strongbox(数论)

本文介绍了一种特殊组合锁的工作原理及其数学特性,并提出一种算法来确定该锁可能的最大开启位置数。通过分析已知非开启及唯一开启位置,利用最大公约数等数学概念,设计程序解决实际问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

描述

Byteasar is a famous safe-cracker, who renounced his criminal activity and got into testing and certifying anti-burglary devices.
He has just received a new kind of strongbox for tests: a combinatorial safe.
A combinatorial safe is something different from a combination safe, even though it is opened with a rotary dial.
The dial can be set in nn different positions, numbered from 0 to n1n−1 .
Setting the dial in some of these positions opens the safe, while in others it does not.
And here is the combinatorial property, from which the name comes from:
if xx and y are opening positions, then so is (x+y) mod n(x+y) mod n too; note that is holds for x=yx=y as well.
Byteasar tried kk different positions of the dial: m1,m2,,mk.
The positions m1,m2,,mk1m1,m2,⋯,mk−1did not open the safe,only the last position mkmkdid.
Byteasar is already tired from checking these kk positions and has thus absolutely no intention of trying the remaining ones.
He would like to know however, based on what he already knows about the positions he tried, what is the maximum possible number of positions that open the safe.
Help him by writing an appropriate program!

输入

The first line of the standard input gives two integers n and kk , separated by a single space, 1k250 000, kn1014k≤n≤1014.
The second line holds kk different integers, also separated by single spaces, m1,m2,,mkm1,m2,⋯,mk, 0mi<n0≤mi<n.
You can assume that the input data correspond to a certain combinatorial safe that complies with the description above.
In tests worth approximately 70% of the points it holds that k1000k≤1000.
In some of those tests, worth approximately 20% of the points, the following conditions hold in addition: n108n≤108 and k100k≤100.

输出

Your program should print out to the first and only line of the standard output a single integer: the maximum number of the dial’s positions that can open the safe.

输入样例

42 5
28 31 10 38 24

输出样例

14

解题思路

首先说明两个结论:

结论1:如果xx是密码,那么gcd(x,n)也是密码。
证明:
d=gcd(x,n)d=gcd(x,n)
由题意,因xx是密码,故 2x%n,3x%n,,kx%n 均是密码
又由贝祖定理推论(不定方程ax+by=cax+by=c有整数解的充要条件是gcd(a,b)|cgcd(a,b)|c ),xk+nc=dxk+nc=d 一定有整数解(未知数是kkc),那么 kZ∃k∈Z 使得kxd(mod n)kx≡d(mod n),故dd也是密码

结论2:如果x,y是密码,那么gcd(x,y)gcd(x,y)也是密码。
证明:
d=gcd(x,y)d=gcd(x,y)
由题意,因x,yx,y是密码,易知 (px+qy)%n(px+qy)%n 也是密码(p,q0p,q⩾0
又由贝祖定理推论,xp+yq=dxp+yq=d一定有整数解(未知数是pqp和q),那么  p,qZ∃ p,q∈Z 使得 px+qyd(mod n)px+qy≡d(mod n),故dd也是密码

再看看这道题,设 x,y 均为密码且xx是所有密码中最小的,若xy,则 gcd(x,y)<xgcd(x,y)<xgcd(x,y)gcd(x,y) 是密码(结论2),与假设矛盾,故 x|yx|y。因此,这组密码为 x,2x,3x,,kx (kx<n)x,2x,3x,⋯,kx (kx<n) ————①
d=gcd(mk,n)d=gcd(mk,n),由于mkmk是密码,根据结论1,dd 也是密码,又由①得:x|d(仍设xx是所有密码中最小的那个)

但是这道题给了一些限制条件:
1. 要求最多有多少密码,故我们希望x尽量的小,密码数量为n/xn/x
2. 给出了k1k−1个不是密码的数字,故 xx 不能整除 m1,m2,,mk1

综上所述,这道题的实现方法是:在根号的时间里暴力枚举处理出 gcd(mk,n)gcd(mk,n) 的所有因数,去除能整除 m1,m2,,mk1m1,m2,⋯,mk−1 中任意一个的数,设剩下的数中最小的为xx,则答案就是 n/x


Code

(洛谷上卡时过了……应该可以优化一下算法)

#include<algorithm>
#include<cstdio>
#include<cmath>

using namespace std;

typedef long long LL;

LL n, k, a[250005], d, q[250005], mn;

LL gcd(LL a, LL b){
    if(a < b)   a ^= b, b ^= a, a ^= b;
    return b == 0 ? a : gcd(b, a % b);
}

int main(){
    scanf("%lld%lld", &n, &k);
    for(int i = 1; i <= k; i++) scanf("%lld", &a[i]);
    d = gcd(a[k], n);
    for(LL i = 1; i <= sqrt(d); i++)
        if(d % i == 0){
            q[++q[0]] = i;
            if(i * i != d)
                q[++q[0]] = d / i;
        }
    sort(q+1, q+q[0]+1);
    for(int i = 1; i < k; i++)
        for(int j = 1; j <= q[0]; j++)
            if(q[j] && a[i] % q[j] == 0)
                q[j] = 0;
    for(mn = 1; mn <= q[0]; mn++)
        if(q[mn])   break;
    printf("%lld\n", n / q[mn]);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值