题目描述

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);
}

BZOJ2277[Poi2011]Strongbox——数论的更多相关文章

  1. bzoj 2277 [Poi2011]Strongbox 数论

    2277: [Poi2011]Strongbox Time Limit: 60 Sec  Memory Limit: 32 MBSubmit: 527  Solved: 231[Submit][Sta ...

  2. bzoj2277 [Poi2011]Strongbox

    2277: [Poi2011]Strongbox Time Limit: 60 Sec  Memory Limit: 32 MBSubmit: 498  Solved: 218[Submit][Sta ...

  3. BZOJ2277 [Poi2011]Strongbox 【数论】

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

  4. 【BZOJ】2277: [Poi2011]Strongbox

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

  5. 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个在 ...

  6. POI2011题解

    POI2011题解 2214先咕一会... [BZOJ2212][POI2011]Tree Rotations 线段树合并模板题. #include<cstdio> #include< ...

  7. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  8. POI做题笔记

    POI2011 Conspiracy (2-SAT) Description \(n\leq 5000\) Solution 发现可拆点然后使用2-SAT做,由于特殊的关系,可以证明每次只能交换两个集 ...

  9. [poi2011]bzoj 2277 —— strongbox·[洛谷3518]

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

随机推荐

  1. ubuntu14.04下播放器SMplayer的安装

    1. Mplayer 与 SMplayer的区别 虽然MPlayer播放器是人类史上最强大的播放器(参数超过千个),但是其默认编译没有界面,所以写参数时间甚至比看片时间还长.虽然编译时候可以选择--e ...

  2. 学习CSS布局 - 没有布局

    如果你只想把所有内容都塞进一栏里,那么不用设置任何布局也是OK的. 然而,如果用户把浏览器窗口调整的很大,这时阅读网页会非常难受: 读完每一行之后,你的视觉焦点要从右到左移动一大段距离. 试着调整下浏 ...

  3. three.js - 渲染并展示三维对象

    看结果: 看源码及解释: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...

  4. SkylineGlobe 邻近度(Proximity)分析JavaScript源代码

    邻近度(Proximity)描述了地理空间中两个地物距离相近的程度,是空间分析的一个重要手段. <html xmlns="http://www.w3.org/1999/xhtml&qu ...

  5. python属性查找 深入理解(attribute lookup)

    在Python中,属性查找(attribute lookup)是比较复杂的,特别是涉及到描述符descriptor的时候. 在上一文章末尾,给出了一段代码,就涉及到descriptor与attribu ...

  6. [05] Bean的作用域和生命周期

    1.Bean的作用域和初始化时间 之前我们稍微提到过,Spring中管理的Bean,默认都是单例模式,这意味着你多次获取某个对象,得到的都是相同的对象.单例作用域的显性写法是scope属性,如下,这和 ...

  7. bat基础知识

    1.打日志:使用重定向 eg:call test.bat>log/test.log 2.不关闭cmd窗口:使用pause eg: 结果: ps:注意,在自动化运维的时候,比如创建自动发版的脚本的 ...

  8. (译)理解 LSTM 网络 (Understanding LSTM Networks by colah)

    @翻译:huangyongye 原文链接: Understanding LSTM Networks 前言:其实之前就已经用过 LSTM 了,是在深度学习框架 keras 上直接用的,但是到现在对LST ...

  9. item 4: 知道怎么去看推导的类型

    本文翻译自modern effective C++,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 博客已经迁移到这里啦 对于推导类型结果的查看,根据不同的软件开发阶段,你想知道的信息的不 ...

  10. WEB 小案例 -- 网上书城(四)

    针对于这个小案例我们今天讲解结账操作,也是有关这个案例的最后一次博文,说实话这个案例的博文写的很糟糕,不知道该如何去表述自己的思路,所以内容有点水,其实说到底还是功力不够. 处理思路 点击结账,发送结 ...