题目

描述

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 nnn different positions, numbered from 0" role="presentation" style="position: relative;">000 to n−1n−1n-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 xxx and y" role="presentation" style="position: relative;">yyy are opening positions, then so is (x+y) mod n(x+y)modn(x+y)\ mod\ n too; note that is holds for x=yx=yx=y as well.
Byteasar tried kkk different positions of the dial: m1,m2,⋯,mk" role="presentation" style="position: relative;">m1,m2,⋯,mkm1,m2,⋯,mkm_1,m_2,\cdots,m_k.
The positions m1,m2,⋯,mk−1m1,m2,⋯,mk−1m_1,m_2,\cdots,m_{k-1}did not open the safe,only the last position mkmkm_kdid.
Byteasar is already tired from checking these kkk 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" role="presentation" style="position: relative;">nnn and kkk , separated by a single space, 1≤k≤250 000" role="presentation" style="position: relative;">1≤k≤250 0001≤k≤250 0001\le k\le 250\ 000, k≤n≤1014k≤n≤1014k\le n\le 10^{14}.
The second line holds kk different integers, also separated by single spaces, m1,m2,⋯,mkm1,m2,⋯,mkm_1,m_2,\cdots,m_k, 0≤mi<n0≤mi<n0\le m_i.
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 k≤1000k≤1000k\le 1000.
In some of those tests, worth approximately 20% of the points, the following conditions hold in addition: n≤108n≤108n\le 10^8 and k≤100k≤100k\le 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:如果xxx是密码,那么gcd(x,n)" role="presentation" style="position: relative;">gcd(x,n)gcd(x,n)gcd(x, n)也是密码。
证明:
设d=gcd(x,n)d=gcd(x,n)d=gcd(x, n)
由题意,因xxx是密码,故 2x%n,3x%n,⋯,kx%n" role="presentation" style="position: relative;">2x%n,3x%n,⋯,kx%n2x%n,3x%n,⋯,kx%n2x\%n,3x\%n,\cdots,kx\%n 均是密码
又由贝祖定理推论(不定方程ax+by=cax+by=cax+by=c有整数解的充要条件是gcd(a,b)|cgcd(a,b)|cgcd(a,b)|c ),xk+nc=dxk+nc=dxk+nc=d 一定有整数解(未知数是kkk和c" role="presentation" style="position: relative;">ccc),那么 ∃k∈Z∃k∈Z\exists k \in Z 使得kx≡d(mod n)kx≡d(modn)kx \equiv d (mod\ n),故ddd也是密码

结论2:如果x,y" role="presentation" style="position: relative;">x,yx,yx,y是密码,那么gcd(x,y)gcd(x,y)gcd(x, y)也是密码。
证明:
设d=gcd(x,y)d=gcd(x,y)d = gcd(x, y)
由题意,因x,yx,yx, y是密码,易知 (px+qy)%n(px+qy)%n(px+qy)\%n 也是密码(p,q⩾0p,q⩾0p,q \geqslant 0)
又由贝祖定理推论,xp+yq=dxp+yq=dxp+yq=d一定有整数解(未知数是p和qp和qp和q),那么 ∃ p,q∈Z∃p,q∈Z\exists \ p,q \in Z 使得 px+qy≡d(mod n)px+qy≡d(modn)px+qy\equiv d(mod\ n),故ddd也是密码

再看看这道题,设 x,y" role="presentation" style="position: relative;">x,yx,yx,y 均为密码且xxx是所有密码中最小的,若x∤y" role="presentation" style="position: relative;">x∤yx∤yx \nmid y,则 gcd(x,y)<xgcd(x,y)<xgcd(x,y)且 gcd(x,y)gcd(x,y)gcd(x,y) 是密码(结论2),与假设矛盾,故 x|yx|yx|y。因此,这组密码为 x,2x,3x,⋯,kx (kx<n)x,2x,3x,⋯,kx(kx<n)x,2x,3x,\cdots,kx \ (kx ————①
设 d=gcd(mk,n)d=gcd(mk,n)d=gcd(m_k,n),由于mkmkm_k是密码,根据结论1,ddd 也是密码,又由①得:x|d" role="presentation" style="position: relative;">x|dx|dx|d(仍设xxx是所有密码中最小的那个)

但是这道题给了一些限制条件:
1. 要求最多有多少密码,故我们希望x" role="presentation" style="position: relative;">xxx尽量的小,密码数量为n/xn/xn/x
2. 给出了k−1k−1k-1个不是密码的数字,故 xxx 不能整除 m1,m2,⋯,mk−1" role="presentation" style="position: relative;">m1,m2,⋯,mk−1m1,m2,⋯,mk−1m_1,m_2,\cdots,m_{k-1}

综上所述,这道题的实现方法是:在根号的时间里暴力枚举处理出 gcd(mk,n)gcd(mk,n)gcd(m_k,n) 的所有因数,去除能整除 m1,m2,⋯,mk−1m1,m2,⋯,mk−1m_1,m_2,\cdots,m_{k-1} 中任意一个的数,设剩下的数中最小的为xxx,则答案就是 n/x" role="presentation" style="position: relative;">n/xn/xn/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;
}

[POI2011] SEJ-Strongbox(数论)相关推荐

  1. BZOJ2277[Poi2011]Strongbox——数论

    题目描述 Byteasar is a famous safe-cracker, who renounced his criminal activity and got into testing and ...

  2. BZOJ 2277 Poi2011 Strongbox 数论

    题目大意:给定n和k个整数,求mod n加法下的群G的一个子群G',满足a[1]~a[k-1]都不在群中而a[k]在群中 首先易证G'一定是一个循环群 证明:显然若a在群中则a的逆元在群中 那么我们就 ...

  3. BZOJ2277 [Poi2011]Strongbox 【数论】

    题目链接 BZOJ2277 题解 orz太难了 如果一个数\(x\)是密码,那么所有\((x,n)\)的倍数都是密码 如果两个数\(x,y\)是密码,那么所有\((x,y)\)的倍数都是密码 那么如果 ...

  4. P3518 [POI2011]strongbox

    https://www.luogu.org/problem/show?pid=3518 问题描述: 有一个密码箱,0 到 n-1中的某些整数是它的密码,且满足:如果a和b都是它的密码,那么(a+b)% ...

  5. 【BZOJ】2277: [Poi2011]Strongbox

    题意 有一个密码箱,\(0\)到\(n-1\)中的某些整数是它的密码.如果\(a\)和\(b\)都是它的密码,那么\((a+b)%n\)也是它的密码(\(a,b\)可以相等).某人试了\(k\)次密码 ...

  6. BZOJ 2277 Poi2011 Strongbox

    题目大意:一个集合A,包含了0~n-1这n个数.另有一个集合B,满足: 1.B是A的子集. 2.如果a.b均在B中,则(a+b)%n也在B中(a=b亦可) 给出k个数ai,前k-1个不在B中,第k个在 ...

  7. BZOJ2277: [Poi2011]Strongbox

    n<=10^14,0~n-1中有一些数是密码,且满足:a是密码,b是密码,那么(a+b)%n也是密码(a,b可相等),现小明试了m<=250000个数,前面都错,最后一个对,问n个数中最多 ...

  8. 数论(一)——素数,GCD,LCM

    这是一个数论系列:) 一.素数 ×费马小定理 Theorem: 设 p 是一个素数,a 是一个整数且不是 p 的倍数,那么 很遗憾,费马小定理的逆定理是不成立的.对 a = 2,满足的非素数 n 是存 ...

  9. 【数论总结】-----励志写好一篇数论总结↖(^ω^)↗//正在施工...未完工

    近期学了学数论,来写一波总结吧. (1)排列组合,比较基础的东西了吧.//只写个概念吧,(逃: 概念:就是从n个不同元素中,任取m(m≤n)个元素并成一组,叫做从n个不同元素中取出m个元素的一个组合: ...

最新文章

  1. Learn OpenGL (十二):投光物
  2. 我所知道的flex布局 —— 上篇
  3. linux c 信号量简介
  4. Testlink1.9.5的安装配置
  5. 29 FI配置-财务会计-外币评估-分配到评估范围和会计核算原则
  6. Fedora/RedHat安装Mysql8.0
  7. linux代理过载命令_我们基于Linux的机器人的未来,数据过载和管理以及更多新闻
  8. 离线抓取域登录缓存hash(mscash)
  9. Python 使用Schema 实现接口或方法的JSON格式参数的高效、优雅校验
  10. Time complexity analysis of algorithms
  11. 装箱拆箱的意义 java_Java中的装箱和拆箱深入理解
  12. 从第一范式(2nf)到第二范式(3nf)_阿里P6面试必备-数据库范式与设计实践实例
  13. python3之http.server模块
  14. 推荐几个无版权图片库,可免费使用
  15. 路由器php系统时间设置时间设置时间设置,4G工业路由器系统设置与时间密码设置...
  16. STM32F1系列之常用外设说明
  17. 杭电ACM-LCY算法进阶培训班-专题训练(强连通分量)
  18. cadence 怎么拼版_一种基于Cadenceskill的快速板卡拼版方法与流程
  19. 操作系统发展历程 (Unix、Linux、windows)
  20. 【Ubuntu18.04系统开启防火墙】

热门文章

  1. python安装pyinotify模块
  2. 新手怎么做副业?新手如何开启副业之路?
  3. 如果人工智能迎来下一个寒冬,你认为会是卡在什么问题上?
  4. linux下运行directx,使用DXVK在Linux上改善您的葡萄酒游戏
  5. 安装ENVI报错“Internal Error 25259. DirectX -9”的解决方法
  6. matlab中函数的定义和使用
  7. excel数据平均分配给多人_如何将excel表格中的一列数字平均分配成几组,分组后每组的平均值大致相同...
  8. Leetcode 2214. Minimum Health to Beat Game [Python]
  9. 08_python_练习题——乘法表
  10. JEPF项目初上手,一把辛酸泪