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. servlet生命周期:

    Servlet生命周期分为三个阶段: 1,初始化阶段  servlet实例创建时调用init()方法,在Servlet的整个生命周期内,init()方法只被调用一次. 2,响应客户请求阶段 调用ser ...

  2. .net 音频转换 .amr 转 .mp3 (ffmpeg转换法)

    最近看来是跟声音干上了啊! 音频转换的第二种方法,这种方法相对第一种来说,要简单的多! 首先,你得下载个“ffmpeg.exe” 插件,然后把它放到你的项目中,如下图: 程序中会调用该文件,以助于转换 ...

  3. log4go折腾

    导包 go get -u github.com/alecthomas/log4go log4go.xml配置 <logging> <filter enabled="true ...

  4. 解决vue跨域问题

    package com.qmtt.config; import java.io.IOException; import javax.servlet.Filter; import javax.servl ...

  5. Redis java操作客服端——jedis

    1. Jedis 需要把jedis依赖的jar包添加到工程中.Maven工程中需要把jedis的坐标添加到依赖. 推荐添加到服务层.happygo-content-Service工程中. 1.1. 连 ...

  6. Ajax的项目搭建

    在搭建Ajax项目之前,首先我们的安装nginx,因为Ajax是基于nginx来运行的, 1.安装nginx 和基本的语法 http://nginx.org/ 上面的nginx的官网,下载直接安装就好 ...

  7. 问题处理:Cannot find module (SNMPv2-TC): At line 10 in /usr/share/snmp/mibs/UCD-DLMOD-MIB.txt

    在执行  php -i |grep redis 时显示以下报错信息(但在phpinfo查看时一切正常): MIB search path: /root/.snmp/mibs:/usr/share/sn ...

  8. java访问数据库步骤详解

    eg1: public static void main(String[] args) throws ClassNotFoundException, SQLException { //第一步:加载JD ...

  9. qt qtableview 样式设置

    转载请注明出处:http://www.cnblogs.com/dachen408/p/7531159.html 1.设置tableview的列宽时,必须先setModel再setColumnWidge ...

  10. vijos 1053 Easy sssp

    描述 输入数据给出一个有N(2 <= N <= 1,000)个节点,M(M <= 100,000)条边的带权有向图. 要求你写一个程序, 判断这个有向图中是否存在负权回路. 如果从一 ...