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. Linux在线安装pip和numpy

    最近写Python需要用到numpy包 运行pip install numpy就会自动安装 一.因此需要先安装pip 1.如果安装的是Python>=2.7.9或者Python>=3.4, ...

  2. spring-bean(三)

    配置方式:通过工厂方法配置bean,通过FactoryBean配置bean 配置形式:基于注解的方式 组件扫描 泛型依赖注入 静态工厂方法 /** * 静态工厂方法:直接调用某一个类的静态方法就可以返 ...

  3. hihocoder1718 最长一次上升子序列

    思路: 对于每个i,分别求1~i和i+1~N两部分的最长下降子序列“拼”起来,最终取最大长度即可.学习了如何使用BIT把LIS问题O(N2)算法优化为O(Nlog(N))的算法. https://ww ...

  4. P1967 货车运输 未完成

    #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #inclu ...

  5. Ant题解

    Description: 一根长度为L厘米的木棒上有N只蚂蚁,每只蚂蚁要么向左走,要么向右走,速度为1厘米/秒.当两只蚂蚁相撞时,他们会同时掉头(掉头时间不计)给出每只蚂蚁距离木棒左端的距离,问多少秒 ...

  6. CCF|中间数|Java

    import java.util.*; public class tyt { public static void main(String[] args) { Scanner in = new Sca ...

  7. iOS 播放本地,网络视频

    /** *  创建媒体播放控制器MPMoviePlayerControlle 可以控制尺寸 * *  @return 媒体播放控制器 */ -(MPMoviePlayerController *)mo ...

  8. windows系统下查看或删除自己电脑的共享文件以及文件夹

    (1)查看所有共享 net share (2)删除指定共享 例如:删除C盘共享 net share C$ /delete     net share 共享名 /delete (/del)

  9. CCS3超长文字显示省略号的方法

    需求:当文本长度溢出包含元素时以省略号结尾 CSS3实现方法: #MyDIV{overflow:hidden;text-overflow:ellipsis;} 示例:<!DOCTYPE html ...

  10. Dreamoon and MRT(二元枚举)

    题目 数轴上有M个点a1.a2....am,另有一个数列p1.p2....pn,(1 ≤ pii ≤ M). 给定d1.d2....dn,对所有的 i (1 ≤ i ≤ n),已知 |api+1 - ...