2277: [Poi2011]Strongbox

Time Limit: 60 Sec  Memory Limit: 32 MB
Submit: 498  Solved: 218
[Submit][Status][Discuss]

Description

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次都失败了,最后一次成功了。
问:该密码箱最多有多少不同的密码。

Input

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

Output

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.

一行,表示结果

Sample Input

42 5
28 31 10 38 24

Sample Output

14
分析:比较难的一道数学题.有两个结论:1.如果x是密码,那么gcd(x,n)也是密码. 2.如果x,y是密码,那么gcd(x,y)也是密码.根据这两个结论就能很轻松地解决本题了.
      先来证明第一个结论:构造二元一次不定方程x*k - n*c = gcd(x,n),这个方程是一定有解的,也就是说一定存在一个 k使得x*k%n = gcd(x,n).而x是密码,(x+x)%n也是密码,所以k*x%n也是密码,那么gcd(x,n)就是密码.x就是题目中告诉的a[k].
      再来证明第二个结论:gcd(x,y) = a*x + b*y,a,b有可能小于0.根据结论一可以推出
(p*x + q*y)%n是密码(p,q ≥ 0). 由a*x + b*y ≡ gcd(x,y)(mod n),变形一下可以得到:
a*x + b*y ≡ a*x + b*y + p*n*x + q*n*y(mod n) --> (a + p*n) * x + (b + q*n) * y ≡ gcd(x,y)(mod n).根据假设,((a + p*n) * x + (b + q*n) * y) % n是密码(x,y系数大于0),那么gcd(x,y)也是密码.
      把所有密码写出来以后,可以发现是一个x,2x,3x......的形式,所以任务就是找到一个最小的x使得x整除gcd(a[k],n).同时这个x不能整除a[j](1 ≤ j < k).那么x就是gcd(a[k],n)的因子,根号的时间处理出来然后进行判断.判断的话也有一个优化,如果y是密码,gcd(x,y)不是密码,那么x也不是密码,所以在判断的时候看一下所有的gcd(a[j],n)是否被当前的因子整除就行了.
#include <cstdio>
#include <cmath>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; typedef long long ll; ll n,k,a[],ans = n,cnt; bool check(ll x)
{
for (int i = ; i <= cnt; i++)
if (a[i] % x == )
return false;
return true;
} ll gcd(ll a,ll b)
{
if (!b)
return a;
return gcd(b,a % b);
} int main()
{
scanf("%lld%lld",&n,&k);
for (int i = ; i <= k; i++)
{
scanf("%lld",&a[i]);
a[i] = gcd(a[i],n);
}
sort(a + ,a + k);
for (int i = ; i < k; i++)
if (a[i] != a[i - ])
a[++cnt] = a[i];
for (ll i = ; i <= sqrt(a[k]); i++)
if (a[k] % i == )
{
if (check(i))
{
ans = n / i;
break;
}
else
if (check(a[k] / i))
ans = n / a[k] * i;
}
printf("%lld\n",ans); return ;
}

bzoj2277 [Poi2011]Strongbox的更多相关文章

  1. BZOJ2277[Poi2011]Strongbox——数论

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

  2. BZOJ2277 [Poi2011]Strongbox 【数论】

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

  3. bzoj 2277 [Poi2011]Strongbox 数论

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

  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. Java 学习列表

    这是从450家企业的招聘信息中统计而来,相对来说还是比较真实的,虽然有些公司的招聘要求万年不变,但还是可以大致反应企业的招聘要求的.

  2. 使用gitblit 在windows平台搭建git服务器

    1.下载jdk,安装并且配置好环境变量 2.下载gitblit 直接解压无需安装 3.配置gitblit 1.修改gitblit安装目录下的data文件下的gitblit.properties.将in ...

  3. Flutter 1.0 正式版: Google 的跨平台 UI 工具包

    今天我们非常高兴的宣布,Flutter 的 1.0 版本正式发布!Flutter 是 Google 为您打造的 UI 工具包,帮助您通过一套代码同时在 iOS 和 Android 上构建媲美原生体验的 ...

  4. 一段字符串中间提取json字符串

    项目过程中经常打日志:LOG.error("[failure][CreateOrder] param:{}", JSON.toJSONString(userCreateOrderD ...

  5. js中json处理总结之JSON.parse

    踩过的坑都将成为路上的风景.队友在cookie中已存以下值: address_info {"address_name":"人民大会堂","...lng ...

  6. TFS2010单独安装配置tfs build server

    记录一下确实很磨人. 同样硬件和软件环境的两台服务器,其中一台服务器很久之前就配置好了tfs2010 build ,然后最近想再配置一台tfs build server,但是按照以前的配置流程始终提示 ...

  7. Java replaceAll不区分大小写

    Java 中replaceAll如何忽略大小写呢? 方式一:在正则表达式前面添加(?i) @Test public void test_replaceAll33(){ String input = & ...

  8. QTabelwidget 添加复选框

    QString sceneName = QString("%1(%2)").arg(sisList[i].sceneName).arg(sisList[i].sceneNo); Q ...

  9. phpstorm 格式化代码

    MAC 安装phpcs.phpcbf composer global require 'squizlabs/php_codesniffer=*' Changed current directory t ...

  10. postman的关联,即如何在请求中引用上次请求返回的值

    做接口测试,一定会遇到这种情况,需要拿上次请求的值在本次请求中使用,比如,我们去测试一个东西,要去登录才能做其他的操作,需要拿到登录返回数据中的某些字段,比如,token啊等... 如果发一次请求,就 ...