2277: [Poi2011]Strongbox

Time Limit: 60 Sec  Memory Limit: 32 MB
Submit: 527  Solved: 231
[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
 

假如x是密码,则所有gcd(x,n)的倍数就一定是密码,反之则一定不是(因为线性方程组的解)

换言之,密码一定可以表示为x,2x,3x,4x,......其中x为n的一个约数

推出了上面这些性质这道题就很好做了,首先最后一次试出来了说明x|gcd(a[k],n)

且x又不能整除gcd(a[i],n),其中i<k

所以我们暴力枚举所有可能的x,然后直接检验是否满足不能整除gcd(a[i],n)就可以了

 #include<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<iostream> #define ll long long
#define inf 1000000007
#define N 1000007 #define Wb putchar(' ')
#define We putchar('\n')
#define rg register int
using namespace std;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-;ch=getchar();}
while(isdigit(ch)){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
}
inline void write(ll x)
{
if(x<) putchar('-'),x=-x;
if (x==) putchar();
rg num=;char c[];
while(x) c[++num]=(x%)+,x/=;
while(num) putchar(c[num--]);
} ll n,k,ans;
ll a[],cnt; bool check(ll x)
{
for(rg i=;i<=cnt;i++)
if(a[i]%x==) return false;
return true;
}
ll gcd(ll a,ll b)
{
if(a==) return b;
return gcd(b%a,a);
}
int main()
{
n=read(),k=read();
for(rg i=;i<=k;i++)
a[i]=read();
ans=n;
for(rg i=;i<=k;i++)
a[i]=gcd(n,a[i]);
sort(a+,a+k);
for(rg i=;i<k;i++)
if(a[i]!=a[i-])
{
cnt++;
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;
}
write(ans);
}

bzoj 2277 [Poi2011]Strongbox 数论的更多相关文章

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

  2. 【BZOJ】2277: [Poi2011]Strongbox

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

  3. BZOJ2277[Poi2011]Strongbox——数论

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

  4. bzoj2277 [Poi2011]Strongbox

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

  5. bzoj 4176 Lucas的数论

    bzoj 4176 Lucas的数论 和约数个数和那题差不多.只不过那个题是多组询问,这题只询问一次,并且 \(n\) 开到了 \(10^9\). \[ \begin{align*} \sum_{i= ...

  6. BZOJ 2530 Poi2011 Party 【枚举】

    BZOJ 2530 Poi2011 Party Description Byteasar intends to throw up a party. Naturally, he would like i ...

  7. [bzoj 2216] [Poi2011] Lightning Conductor

    [bzoj 2216] [Poi2011] Lightning Conductor Description 已知一个长度为n的序列a1,a2,-,an. 对于每个1<=i<=n,找到最小的 ...

  8. Mobius反演与积性函数前缀和演学习笔记 BZOJ 4176 Lucas的数论 SDOI 2015 约数个数和

    下文中所有讨论都在数论函数范围内开展. 数论函数指的是定义域为正整数域, 且值域为复数域的函数. 数论意义下的和式处理技巧 因子 \[ \sum_{d | n} a_d = \sum_{d | n} ...

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

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

随机推荐

  1. python3【基础】-集合

    集合( set):把不同的元素组成一起形成集合,是python基本的数据类型. 集合元素(set elements):组成集合的成员(不可重复) class set(object) | set() - ...

  2. WCF传送大数据时的错误“ 超出最大字符串内容长度配额”

    格式化程序尝试对消息反序列化时引发异常: 尝试对参数 http://tempuri.org/ 进行反序列化时出错: GetLzdtArticleResult.InnerException 消息是“反序 ...

  3. 【探路者】互评beta版本

    成员博客 1蔺依铭:http://www.cnblogs.com/linym762/ 2张恩聚:http://www.cnblogs.com/zej87/ 3米赫:http://www.cnblogs ...

  4. 软件工程-东北师大站-第四次作业PSP

    1.本周PSP 2.本周进度条 3.本周累计进度图 代码累计折线图 博文字数累计折线图 4.本周PSP饼状图

  5. Java变量声明,实例化,问题

    1.变量在输出前必须实例化,这是因为只有声明,没有分配内存空间 在这种情况下会报错 2.实例化后,尽管没有赋值,可能是默认了吧,但也不会输出null,什么也没有输出 上面的理解可能是错的,a赋值了,就 ...

  6. KNN算法之图像处理二

    1.看了诸多博客,初步得到结论是:KNN不适合做图像分类. 2.如果偏要用此方法进行图像分类,距离计算为:对应的每个像素代表的像素值进行绝对差值计算,最后求和.这就是“图像的距离”

  7. Hadoop环境搭建01

    根据马士兵老师的Hadoop进行的配置 1.首先列下来需要用到的软件 VirtulBox虚拟机.Centos7系统镜像.xshell.xftp.jdk安装包.hadoop-2.7.0安装包 2.在Vi ...

  8. struts2 action json(还有servlet的)

    http://yshjava.iteye.com/blog/1333104 留着 版权声明:本文为博主原创文章,未经博主允许不得转载.

  9. Lucene 分词

    在Lucene中很多数据是通过Attribute进行存储的 步骤是同过TokenStrem获取文本信息流 TokenStream stream = a.tokenStream("conten ...

  10. Struts2(五)

    以下内容是基于导入struts2-2.3.32.jar包来讲的 1.文件上传 A.单文件上传 <body> <form action="${pageContext.requ ...