博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数字任意组合 - gcd
阅读量:7142 次
发布时间:2019-06-28

本文共 3823 字,大约阅读时间需要 12 分钟。

链接: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 
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
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 
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
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
:: iterator it; for(it = s.begin(); it != s.end(); it++) { printf("%d ", *it); } return 0;}

 

转载于:https://www.cnblogs.com/ccut-ry/p/9495472.html

你可能感兴趣的文章
本地YUM-Redhat Enterprise 5.9
查看>>
客户端扫码登录功能的实现思路
查看>>
anroid Bar 进度条示例
查看>>
照猫画虎学UML-泛化、关联、聚合、组合、依赖
查看>>
常见的网页报错
查看>>
linux学习第5天
查看>>
京东市场份额下降 Q2交易额究竟有多少
查看>>
用myeclipse创建servlet
查看>>
搭建PXE网络实现远程装机服务
查看>>
pyinstaller打包资源文件及包含eyed3模块时提示找不到libmagic处理
查看>>
清空密码框中的内容
查看>>
Microsoft Hyper-V Server 2012快速上手之前奏
查看>>
DMAR:[fault reason 06] PTE Read access is not set
查看>>
Qt第二课 QtCreator下载安装
查看>>
句子逆序
查看>>
巴厘岛最全的美食攻略
查看>>
eclipse svn插件中英文切换
查看>>
C#学习常用命名空间【1000】---System.Reflection 之MethodInfo(方法信息类)
查看>>
Android Fragment完全解析,关于碎片你所需知道的一切
查看>>
map存储格式
查看>>