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. JAVA学习笔记--正则表达式

    正则表达式是一种强大而灵活的文本处理工具.使用正则表达式,可以让我们以编程的方式构造复杂的文本,并对输入的字符串进行搜索. 一.基础正则表达式语法(表格来自J2SE6_API) 字符 x 字符 x \ ...

  2. 华为笔试——C++特定位数比较

    题目:特定位数比较 题目介绍:输入两行数据,第一行为 m 个正整数,以空格隔开:第二行为正整数 n ,且 n<= m:要求对第一行的数字的后三位大小进行排序,输出排行 n 的数字,其中,若不满三 ...

  3. linux 性能分析命令及其解释

    很多时候,我们需要对linux上运行的环境大体有一个了解,那么久需要大体知道当前系统的相关资源的使用情况,那么可以用一些linux提供的丰富的命令来查看 性能分析 vmstat 虚拟内存统计 用法 U ...

  4. javascript提高篇

    本章简介 本章内容比较少,有三个分享的知识.你可能都看过了,因为网上也有很多提问和解答,如果没看过或者没搞懂,你可以再看看这篇文章. 1. 数组去重方法的演变    --  走向代码缩短化 2. [] ...

  5. 作业MathExamV2.0

    MathExam233 211614269 林凯 211601233张康凌 一.预估与实际 PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时( ...

  6. 团队介绍 you i

    我们团队一共四个人,我们足够了解对方的优缺点,能够很好的进行交流沟通.对于一些问题也能有好的方法去解决,我做事情比较讲究高效和尽可能的完美,或者说要做到我自己觉得完美,才会停下来.对于一件事情,我有自 ...

  7. firefox插件Firebug的使用教程

    什么是Firebug 从事了数年的Web开发工作,越来越觉得现在对WEB开发有了更高的要求.要写出漂亮的HTML代码:要编写精致的CSS样式表展示每个页面模块:要调试 javascript给页面增加一 ...

  8. lintcode-442-实现 Trie

    442-实现 Trie 实现一个 Trie,包含 insert, search, 和 startsWith 这三个方法. 注意事项 你可以假设所有的输入都是小写字母a-z. 样例 insert(&qu ...

  9. JavaScript数组去重的四种方法

    今天,洗澡的想一个有趣的问题,使用js给数组去重,我想了四种方法,虽然今天的任务没有完成,5555: 不多说,po代码: //方法一:简单循环去重    Array.prototype.unique1 ...

  10. 【final】140字互评②

    按照产品发布顺序 nice!----约吧 我们的团队展示相对于上次的手足无措,有了一定进步.但是整体还是不那么流畅总结起来的缺点是: 1.发布时,摄像头不清晰 且抖动 我们没有把摄像头固定,并且为了让 ...