题目描述

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 different positions, numbered from 0 to n-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 x and y are opening positions, then so is (x+y) mod n too; note that is holds for x=y as well.
Byteasar tried k different positions of the dial: m1,m2….mk. The positions M1,M 2….Mk-1 did not open the safe, only the last position Mk did. Byteasar is already tired from checking these K 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!

有一个密码箱,0到n-1中的某些整数是它的密码。
且满足,如果a和b都是它的密码,那么(a+b)%n也是它的密码(a,b可以相等)
某人试了k次密码,前k-1次都失败了,最后一次成功了。
问:该密码箱最多有多少不同的密码。

输入

The first line of the standard input gives two integers N and k, separated by a single space, (1<=K<=250000,k<=N<=10^14), The second line holds K different integers, also separated by single spaces, m1,m2….mk, 0<=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 k<=1000. In some of those tests, worth approximately 20% of the points, the following conditions hold in addition: N< 10 ^8 and K<=100.

第一行n,k
下面一行k个整数,表示每次试的密码
保证存在合法解

1<=k<=250000 k<=n<=10^14

输出

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
  如果x,y是密码,那么gcd(x,y)的倍数就都是密码(证明在最后)。同理,如果x是密码,那么2x,3x,4x……kx都是密码,那么gcd(x,n)的倍数就都是密码,反之则一定不是。因为前k-1次都没试出来,所以gcd(a[i],n)(i<k)就都不是密码,假设x是密码,那么x一定不是gcd(a[i],n)的约数,又因为gcd(a[k],n)是密码,所以x一定是gcd(a[k],n)的约数,枚举gcd(a[k],n)的约数验证,取n/x最大的就好了。为什么两个数i,j都满足但答案不能是n/i+n/j?因为如果i,j互质,那么gcd(i,j)=1,这样所有n内的数就都是密码了,显然不行。如果i,j不互质,那么gcd(i,j)的答案在前面已经被计算过了,n/i+n/j会重复。
证明:
设d=gcd(x,y),x=a*d,y=b*d,因为px+qy一定是密码(p,q>0),所以d*(ap+bq)就一定是密码。而gcd(x,y)的任意倍数是d*(ap'+bq')其中p',q'不一定是正数,那么只要保证(ap'+bq')在%n意义下且在p',q'>0时能表示所有正数就好了。因为a,b互质,所以ap'+bq'=1一定有解,但其中有一个一定是负数,只要把那个数一直+n直到为正就好了。加一个数的n倍%n结果不变。再把上述二元一次方程左右两边扩大任意倍数,就能表示d的任意倍了。
#include<map>
#include<set>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int k;
int flag;
long long ans;
long long n;
long long a[250010];
long long gcd(long long x,long long y)
{if(y==0){return x;}return gcd(y,x%y);
}
int cnt=0;
bool check(long long x)
{for(int i=1;i<=cnt;i++){if(a[i]%x==0){return false;}}return true;
}
int main()
{long long y;scanf("%lld%d",&n,&k);for(int i=1;i<=k;i++){scanf("%lld",&a[i]);a[i]=gcd(a[i],n);}long long x=a[k];ans=n;sort(a+1,a+k);for(int i=1;i<k;i++){if(a[i]!=a[i-1]){a[++cnt]=a[i];}}for(long long i=1;i*i<=x;i++){if(x%i==0){if(check(i)){ans=n/i;break;}else if(check(a[k]/i)){ans=n/a[k]*i;}}}printf("%lld",ans);
}

转载于:https://www.cnblogs.com/Khada-Jhin/p/9590059.html

BZOJ2277[Poi2011]Strongbox——数论相关推荐

  1. BZOJ2277 [Poi2011]Strongbox 【数论】

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

  2. BZOJ2277: [Poi2011]Strongbox

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

  3. BZOJ 2277 Poi2011 Strongbox 数论

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

  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. [POI2011] SEJ-Strongbox(数论)

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

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

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

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

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

最新文章

  1. ubuntu 中 pip 出现 ModuleNotFoundError: No module named ‘pip._internal‘
  2. 京沪深月薪均超2万元,清华近三成毕业生年入50万+ | 2019上半年中高端人才就业现状报告...
  3. Linux安装redis最新版5.0.8
  4. html瀑布式原理,纯css3+html瀑布流效果
  5. Nginx学习之HTTP/2.0配置
  6. GPU 原理解密(一)画个三角形居然这么难
  7. 程序的加载和执行(三)——《x86汇编语言:从实模式到保护模式》读书笔记23
  8. atcoder A - Frog 1(DP)
  9. Gartner:2025年有效细分市场中过半企业的 IT 支出将转向云
  10. 对 app_offline.htm 的几点错误认识
  11. Java开发知识之Java的包装类
  12. 2020五一建模C题:饲料混合加工问题完整论文(附录有代码)
  13. [EXUI][原创]菜单简单创建和点击事件的触发
  14. java poi 合并单元格 边框显示不全
  15. UltraEdit 注册机使用激活方法:
  16. 杭电、POJ、ZOJ ACM刷题顺序和题目分类详解
  17. 通信常识:波特率、数据传输速率与带宽的相互关系
  18. 单(两)个正态总体的统计量的分布相关定理
  19. u-boot-2016.09 make工具之fixdep
  20. ems与nms_告警处理方法、装置、nms、oss及ems的制作方法

热门文章

  1. CSS---padding详解
  2. 【Python】定时获取卫星地球图像作为电脑壁纸
  3. 欧式端子 管型端子 管形接线端子 插针 規格/尺寸
  4. 要做好网站,你需要这三点
  5. windows中的出站和入站规则
  6. 程序员怎么高效做笔记
  7. [Mysql] SQL的书写顺序与执行顺序
  8. SQL 对含有字母和数字的列排序
  9. 【VJudge】【Legilimens Contest 1】
  10. 支付宝php支付接口说明