更好的阅读体验

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. Formform

    知识预览 一 Django的form组件 二 Django的model form组件 三 Django的缓存机制 四 Django的信号 五 Django的序列化 回到顶部 一 Django的form ...

  2. Hyper-V虚拟机win7网络红叉,无法上网解决方法

    之前一直都是玩Vmware虚拟机,后来win8之后的系统有Hyper-V虚拟机就开始接触了. Windows 中内置的Hyper-V管理器可以说是给很多人带来了惊喜!至少运行的流畅程度要比Vmware ...

  3. 什么是javascript字面量,常量,变量,直接量?

    1.字面量是变量的字符串表示形式.它不是一种值,而是一种变量记法. var a = 1 //1是字面量 var b = 'css' //css是字面量 var c = [5,6,7] //567是字面 ...

  4. 声学回声消除(Acoustic Echo Cancellation)

    回声就是声音信号经过一系列反射之后,又听到了自己讲话的声音,这就是回声.一些回声是必要的,比如剧院里的音乐回声以及延迟时间较短的房间回声:而大多数回声会造成负面影响,比如在有线或者无线通信时重复听到自 ...

  5. windows下bower init 报错: bower ENOINT Register requires an interactive shell

    windows下bower初始化时不应该在git bash中,而应该在cmd下打开的dos窗口中进行

  6. openssl之DH(Diffie–Hellman)加密

    //加密机制初始化 g_HDMgr.init(); //对方的public key BIGNUM* peerPubKey = NULL; peerPubKey = BN_bin2bn((unsigne ...

  7. 21.Nginx代理缓存

    1.环境准备 操作系统 应用服务 外网地址 内网地址 CentOS7.6 LB01 10.0.0.5 172.16.1.5 CentOS7.6 Web01 10.0.0.7 172.16.1.7 2. ...

  8. 玩转OneNET物联网平台之简介

    授人以鱼不如授人以渔,目的不是为了教会你具体项目开发,而是学会学习的能力.希望大家分享给你周边需要的朋友或者同学,说不定大神成长之路有博哥的奠基石... QQ技术互动交流群:ESP8266&3 ...

  9. 使用C++代码打印数字正方形

    使用C++代码打印数字正方形 作为一名初学者,最近在跟着网课学习C++程序设计基础.在学习过程中遇到了一些习题,我根据自己的理解和思路写了一些代码实现,算是对自己学习过程的一个记录,也希望可以对别人有 ...

  10. django-模板之过滤器Add(十三)

    1.add 若前后类型不匹配,就返回空. 其他的一些过滤器: first:返回列表的第一个值: last:返回列表的最后一个值: length:返回变量值的长度: linebrakebr:将纯文本中的 ...