更好的阅读体验

Portal

Portal1: Codeforces

Portal2: Luogu

Description

Jeff's got n cards, each card contains either digit 0, or digit 5. Jeff can choose several cards and put them in a line so that he gets some number. What is the largest possible number divisible by 90 Jeff can make from the cards he's got?

Jeff must make the number without leading zero. At that, we assume that number 0 doesn't contain any leading zeroes. Jeff doesn't have to use all the cards.

Input

The first line contains integer \(n (1 \le n \le 103)\). The next line contains \(n\) integers \(a_1, a_2, \cdots , a_n (a_i = 0 or a_i = 5)\). Number ai represents the digit that is written on the \(i\)-th card.

Output

In a single line print the answer to the problem - the maximum number, divisible by 90. If you can't make any divisible by 90 number from the cards, print -1.

Sample Input1


4
5 0 5 0

Sample Output1

0

Sample Input2

11
5 5 5 5 5 5 5 5 0 5 5

Sample Output2

5555555550

Sample Explain

In the first test you can make only one number that is a multiple of 90 - 0.

In the second test you can make number \(5555555550\), it is a multiple of 90.

Description in Chinese

Jeff有一些数字卡片,上面为\(0\)或\(5\)。Jeff想用这些卡片组成一个数字,使这个数字可以被\(90\)整除并且尽可能的大。

Solution

先分解质因数:\(90 = 9 \times 10\)

为什么不分成\(90 = 2 \times 3^2 \times 5\)呢,因为\(9\)有自己的整除特性:各个数位之和被\(9\)整除。

\(10\)的整除特性就不用说了。

然后分类讨论:

  1. 当\(0\)的个数为\(0\),也就是没有\(0\),我们就直接输出\(-1\),因为无论怎么取都不能被\(10\)整除。

  2. 当\(5\)的个数\(\le 9\),而且存在\(0\)时,我们只能构造出\(9 | 0\)(这里的\(|\)表示整除),比如样例\(1\)。

  3. 当\(5\)的个数\(\ge 9\),而且存在\(0\)时,就可以构造能被\(90\)的数,比如样例\(2\)。

我们可以把\(5\)的个数\(\div 9\)取整后\(\times 9\)就得到所要输出的\(5\)的个数了,这个数一定是最大的能被9整除的数,当然最后要把所有的\(0\)输出。

Code

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath> using namespace std; int n, x, sum1, sum2;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &x);
if (x == 5) sum1++; else sum2++;//统计0和5的数量
}
if (sum2 == 0) {//情况1
printf("-1\n");
return 0;
}
if (sum1 < 9) {//情况2
printf("0\n");
return 0;
}
sum1 = sum1 / 9 * 9;//将5的个数÷9整除后×9,得到最终的5的个数
for (int i = 1; i <= sum1; i++)
printf("5");
for (int i = 1; i <= sum2; i++)
printf("0");
printf("\n");
return 0;
}

『题解』Coderforces352A Jeff and Digits的更多相关文章

  1. 『题解』洛谷P1063 能量项链

    原文地址 Problem Portal Portal1:Luogu Portal2:LibreOJ Portal3:Vijos Description 在\(Mars\)星球上,每个\(Mars\)人 ...

  2. 『题解』Codeforces121A Lucky Sum

    更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description Petya loves lucky numbers. Everybody k ...

  3. 『题解』Codeforces1142A The Beatles

    更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description Recently a Golden Circle of Beetlovers ...

  4. 『题解』Codeforces1142B Lynyrd Skynyrd

    更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description Recently Lynyrd and Skynyrd went to a ...

  5. 『题解』洛谷P1993 小K的农场

    更好的阅读体验 Portal Portal1: Luogu Description 小\(K\)在\(\mathrm MC\)里面建立很多很多的农场,总共\(n\)个,以至于他自己都忘记了每个农场中种 ...

  6. 『题解』洛谷P2296 寻找道路

    更好的阅读体验 Portal Portal1: Luogu Portal2: LibreOJ Description 在有向图\(\mathrm G\)中,每条边的长度均为\(1\),现给定起点和终点 ...

  7. 『题解』洛谷P1351 联合权值

    更好的阅读体验 Portal Portal1: Luogu Portal2: LibreOJ Description 无向连通图\(\mathrm G\)有\(n\)个点,\(n - 1\)条边.点从 ...

  8. 『题解』Codeforces656E Out of Controls

    更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description You are given a complete undirected gr ...

  9. 『题解』洛谷P2170 选学霸

    更好的阅读体验 Portal Portal1: Luogu Description 老师想从\(N\)名学生中选\(M\)人当学霸,但有\(K\)对人实力相当,如果实力相当的人中,一部分被选上,另一部 ...

随机推荐

  1. 网络游戏开发-客户端4 关于Egret的本地坐标和舞台坐标

    因为最近公司事情比较多,所以没怎么更新博客. 不过咱们这个游戏还是在继续往下写. 今天晚上打算写斗地主的出牌动画,遇到一个问题,就是关于本地坐标和舞台坐标的计算问题 在Egret官网的解释是:x 和 ...

  2. wx.navigateTo、wx.redirectTo、wx.reLaunch、wx.switchTab和wx.navigateBack的区别

    wx.navigateTo.wx.redirectTo.wx.reLaunch.wx.switchTab和wx.navigateBack有什么区别呢? **wx.navigateTo:** 用于保留当 ...

  3. centos7 安装 mysql5.7 版本(全)

    centos 安装 版本说明 :centos7,mysql5.7 ,不是 centos7 可能有些命令不兼容 安装 mysql-server # 下载并安装 mysql yum wget -i -c ...

  4. Zookeeper未授权访问测试

    前言 ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件.它是一个为分布式应用提供一致性服务的软件,提 ...

  5. 经验分享:程序员如何快速定位问题(BUG)

    让我掉下眼泪的 不止内存泄漏 让我夜夜不眠的 不止你的需求 明天还要改多久 你攥着我的手 让我感到为难的 是善变的需求 发布总是在半夜 回滚是永远的愁 错误(Bug)随时的暴漏 困扰着我心头 作为程序 ...

  6. 事业单位招聘网搭建思路和seo方法

    期望目标 自动采集事业单位招聘信息+自动发布到网站+自动提交网址到百度+自动发外链. 技术框架 前端用layUI,后端用flask,数据库用的elasticsearch,编程语言用Python 3.7 ...

  7. eclipse中最有用的10个快捷键

    这里列出一些在使用eclipse的过程中最有用的10个快捷键,通过灵活使用这些快捷键可以提高开发效率和开发质量. 1. [ctrl+shift+r]打开资源 这可能是所有快捷键中最省时间的了.这个快捷 ...

  8. opencv::自定义角点检测

    #include <opencv2/opencv.hpp> #include <iostream> #include <math.h> using namespac ...

  9. 2.单核CPU是如何实现多进程的?

    单核cpu之所以能够实现多进程,主要是依靠于操作系统的进程的调度算法 如时间片轮转算法,在早期,举例说明:有5个正在运行的程序(即5个进程) :   QQ    微信    有道词典      网易云 ...

  10. 13.Linux文件查找-find命令

    find 命令的基本语法如下: 命令 路径 选项 表达式 动作 find [path...] [options] [expression] [action] 查找 地区 妹纸 18-25岁 约? (1 ...