链接:https://www.nowcoder.com/acm/contest/160/A
来源:牛客网

题目描述
有一个计数器,计数器的初始值为0,每次操作你可以把计数器的值加上a1,a2,...,an中的任意一个整数,操作次数不限(可以为0次),问计数器的值对m取模后有几种可能。
输入描述:

第一行两个整数n,m
接下来一行n个整数表示a1,a2,...,an
1≤n≤100
1≤m,a1,a2,...,an≤1000000000

输出描述:

输出一个整数表示答案

输入

3 6
6 4 8

输出

3

题意 : 将所给的几个数字任意组合,再将组合出来的数对 M 取模,求模数的种类数

思路分析 : 解法很巧妙的利用了 gcd , 求一下所有数字的 gcd , 因为是要求个数,再将所求 gcd 与 m 求一次 gcd ,最后 m / gcd 即为答案

    若两个数 gcd 为 1, 则可以通过对一个数取模,拼凑出模数以内的任意数,想一想就明白了

代码示例 :

#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5+5;
#define ll long long ll a[105];
ll n, m;
ll gcd(ll a, ll b) {return b==0?a:gcd(b, a%b); }
set<ll>s; int main () { cin >> n >> m;
ll g = 0;
for(ll i = 1; i <= n; i++){
scanf("%lld", &a[i]);
g = gcd(g, a[i]);
}
ll g2 = gcd(g, m);
printf("%lld\n", m/g2);
return 0;
}

Astronaut Natasha arrived on Mars. She knows that the Martians are very poor aliens. To ensure a better life for the Mars citizens, their emperor decided to take tax from every tourist who visited the planet. Natasha is the inhabitant of Earth, therefore she had to pay the tax to enter the territory of Mars.

There are n
banknote denominations on Mars: the value of i-th banknote is ai

. Natasha has an infinite number of banknotes of each denomination.

Martians have k
fingers on their hands, so they use a number system with base k. In addition, the Martians consider the digit d (in the number system with base k) divine. Thus, if the last digit in Natasha's tax amount written in the number system with the base k is d

, the Martians will be happy. Unfortunately, Natasha does not know the Martians' divine digit yet.

Determine for which values d Natasha can make the Martians happy.

Natasha can use only her banknotes. Martians don't give her change.
Input

The first line contains two integers n
and k (1≤n≤100000, 2≤k≤100000

) — the number of denominations of banknotes and the base of the number system on Mars.

The second line contains n
integers a1,a2,…,an (1≤ai≤109

) — denominations of banknotes on Mars.

All numbers are given in decimal notation.
Output

On the first line output the number of values d

for which Natasha can make the Martians happy.

In the second line, output all these values in increasing order.

Print all numbers in decimal notation.
Examples
Input
Copy

2 8
12 20

Output
Copy

2
0 4

Input
Copy

3 10
10 20 30

Output
Copy

1
0

Note

Consider the first test case. It uses the octal number system.

If you take one banknote with the value of 12
, you will get 148 in octal system. The last digit is 48

.

If you take one banknote with the value of 12
and one banknote with the value of 20, the total value will be 32. In the octal system, it is 408. The last digit is 08

.

If you take two banknotes with the value of 20
, the total value will be 40, this is 508 in the octal system. The last digit is 08

.

No other digits other than 08
and 48 can be obtained. Digits 08 and 48

could also be obtained in other ways.

The second test case uses the decimal number system. The nominals of all banknotes end with zero, so Natasha can give the Martians only the amount whose decimal notation also ends with zero.

题意 :基本同上面的题,只不过数据范围不太一样,并且此题需要输出不同的模数是什么
思路分析 :基本同上,加个 set 即可

代码示例 :

#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5+5;
#define ll long long ll n, k;
ll gcd(ll a, ll b) {return b==0?a:gcd(b, a%b); }
set<ll>s; int main () {
ll x; cin >> n >> k;
ll g = 0;
for(ll i = 1; i <= n; i++){
scanf("%lld", &x);
g = gcd(g, x);
}
for(ll i = 0, j = 0; i <= k; i++, j += g){
s.insert(j%k);
}
printf("%d\n", s.size());
set<ll>:: iterator it; for(it = s.begin(); it != s.end(); it++) {
printf("%d ", *it);
}
return 0;
}

数字任意组合 - gcd的更多相关文章

  1. java中获取字母和数字的组合

    package com.ccytsoft.wkc.util; import java.util.ArrayList; import java.util.List; import java.util.R ...

  2. sqlserver 2008存储过程 多个可空条件任意组合

    很多程序员在实际开发中,经常遇到这种情况,列表上方有很多条件,包含下拉框,输入框等等,这些条件都可以不输入,如果我们需要写一个存储过程,很多条件挨个判断是否为空,且进行任意组合,任何一个开发人员都会疯 ...

  3. python 给定数组任意组合等于一个定值的所有解

    抛出问题: 求给定数组任意组合等于一个定值的所有解 例如列表l = [1, 2, 3, 4, 5],求任意组合的结果为10的所有答案 问题分析: 实际就是列表的所有排列组合,然后算出每个排列组合的值, ...

  4. php如何判断字符串是否是字母和数字的组合

    转载自百度 /其实判断是否是字母和数字或字母数字的组合还可以用PHP ctype_alnum函数 if(!ctype_alnum($vipurl)){ echo '只能是字母或数字的组合';exit; ...

  5. 定义一个类:实现功能可以返回随机的10个数字,随机的10个字母, 随机的10个字母和数字的组合;字母和数字的范围可以指定,类似(1~100)(A~z)

    #习题2:定义一个类:实现功能可以返回随机的10个数字,随机的10个字母, #随机的10个字母和数字的组合:字母和数字的范围可以指定 class RandomString(): #随机数选择的范围作为 ...

  6. 关于1-n任意的gcd的和

    gcd和 题目 GCD sum 公约数的和 大意是让你求1-n任意两个数的gcd的和之类的. 解法 显然你需要枚举对吧,不然你怎么可能求出gcd呢? 其次我们需要一些数学推理 令F(n)表示\(\su ...

  7. 随机产生字母a--z, A-Z 的任意组合

    VERSION 1.0    引自: http://www.coderanch.com/t/134491/Security/generating-secure-tokens package demo; ...

  8. 删除一个数字之后数列gcd最大

    ★实验任务 顾名思义,互质序列是满足序列元素的 gcd 为 1 的序列.比如[1,2,3], [4,7,8],都是互质序列.[3,6,9]不是互质序列.现在并不要求你找出一个互质 序列,那样太简单了! ...

  9. 【BZOJ 3505】 [Cqoi2014]数三角形 容斥原理+排列组合+GCD

    我们先把所有三角形用排列组合算出来,再把一行一列上的三点共线减去,然后我们只观察向右上的三点共线,向左上的乘二即可,我们发现我们如果枚举所有的两边点再乘中间点的个数(GCD),那么我们发现所有的两边点 ...

随机推荐

  1. Vue 父组件往子组件传递方法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. win10 uwp 使用 Border 布局

    在 UWP 写一个界面需要将元素排列,在排列元素的时候有特殊的元素叫容器.容器的意思是元素里面包含元素,在 UWP 用到的容器有很多,也可以自己写一个.简单的容器是 Border 边框控件,如控件说的 ...

  3. vue-cli起项目步骤

    1.全局安装 vue-cli 老版本:npm install -g vue-cli  (npm uninstall vue-cli -g 卸载) 新版本:npm install -g @vue/cli ...

  4. Linux内存页大小

    当使用内存时, 记住一个内存页是 PAGE_SIZE 字节, 不是 4KB. 假定页大小是 4KB 并且 硬编码这个值是一个 PC 程序员常见的错误, 相反, 被支持的平台显示页大小从 4 KB 到 ...

  5. Linux 内核 低级 sysfs 操作

    kobject 是在 sysfs 虚拟文件系统之后的机制. 对每个在 sysfs 中发现的目录, 有一个 kobject 潜伏在内核某处. 每个感兴趣的 kobject 也输出一个或多个属性, 它出现 ...

  6. dotnet 获取指定进程的输入命令行

    本文告诉大家如何在 dotnet 获取指定的进程的命令行参数 很多的程序在启动的时候都需要传入参数,那么如何拿到这些程序传入的参数? 我找到两个方法,一个需要引用 C++ 库支持 x86 和 x64 ...

  7. Checkpoint R77.30 web ui登录配置

    R77.30默认只能使用微软内核的浏览器进行web ui的登陆,在专家模式下修改后chrome和firefox正常登录,步骤如下: [Expert@BJ-ZHX-FW:0]# cp /web/htdo ...

  8. IDE、Cmake、makefile、make

    makefile :就是一个类似脚本的文件,根据一系列规则用于决定哪些文件先编译,哪些文件重新编译等等.甚至于进行更复杂的功能操作,而且还可以执行操作系统的命令.makefile带来的好处就是——“自 ...

  9. 一篇文章带你了解 ZooKeeper 架构

    上一篇文章,我们讲解了 ZooKeeper 入门知识,这篇文章主要讲解下 ZooKeeper 的架构,理解 ZooKeeper 的架构可以帮助我们更好地设计协同服务. 首先我们来看下 ZooKeepe ...

  10. 小白学 Python 爬虫(30):代理基础

    人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...