【Codeforces 375A】Divisible by Seven
【链接】 我是链接,点我呀:)
【题意】
让你把一个包含数字1,6,8,9的数字重新组合,使得组合成的数字能被7整除
【题解】
我们先提取出来1,6,8,9各1个
然后把剩余的len-4个数字除了0之外放在前面
那么这len-4个除了0之外的数字组成的十进制数字对7的取余结果肯定是0~6之间。
然后我们用这个取余结果,和1,6,8,9的所有全排列一个一个去试,尝试把排列出来的结果放在这len-4个数字的后面
最后发现取余结果为0~6都能找到一个1,6,8,9的排列,使得它放在他们后面,然后取余结果为0
最后把剩余的0放在1,6,8,9的后面就好。
【代码】
import java.io.*;
import java.util.*;
public class Main {
static InputReader in;
static PrintWriter out;
public static void main(String[] args) throws IOException{
//InputStream ins = new FileInputStream("E:\\rush.txt");
InputStream ins = System.in;
in = new InputReader(ins);
out = new PrintWriter(System.out);
//code start from here
new Task().solve(in, out);
out.close();
}
static int N = 50000;
static class Task{
int cnt[] = new int[10];
int a[] = {0,1,6,8,9};
int b[] = new int[10];
boolean bo[] = new boolean[5];
int n;
String s;
int cur = 0;
boolean dfs(int dep,int now) {
if (dep==5) {
int temp = now;
for (int i = 1;i <= 4;i++) {
temp = temp*10 + b[i];
temp = temp%7;
}
if (temp==0) {
for (int i = 1;i <= 4;i++) {
out.print(b[i]);
}
return true;
}
return false;
}
for (int i = 1;i <= 4;i++)
if (!bo[i]) {
bo[i] = true;
b[dep] = a[i];
if (dfs(dep+1,now)) return true;
bo[i] = false;
}
return false;
}
public void solve(InputReader in,PrintWriter out) {
s = in.next();
for (int i = 0;i < (int)s.length();i++) {
int key = s.charAt(i)-'0';
if (key==1 && cnt[1]==0) {
cnt[1] = 1;
}else if (key==6 && cnt[6]==0){
cnt[6] = 1;
}else if (key==8 && cnt[8]==0) {
cnt[8] = 1;
}else if (key==9 && cnt[9]==0) {
cnt[9] = 1;
}else {
cnt[key]++;
if (key!=0) {
out.print(key);
cur = cur * 10 + key;
cur = cur % 7;
}
}
}
dfs(1,cur);
for (int i = 1;i <= cnt[0];i++) {
out.print(0);
}
}
}
static class InputReader{
public BufferedReader br;
public StringTokenizer tokenizer;
public InputReader(InputStream ins) {
br = new BufferedReader(new InputStreamReader(ins));
tokenizer = null;
}
public String next(){
while (tokenizer==null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(br.readLine());
}catch(IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
【Codeforces 375A】Divisible by Seven的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【20.23%】【codeforces 740A】Alyona and copybooks
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【34.88%】【codeforces 569C】Primes or Palindromes?
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 546D】Soldier and Number Game
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
随机推荐
- 慕课网2-5 编程练习:通过jQuery通配符选择器进行文字变色
2-5 编程练习 请请使用*选择器将div标签中的字体颜色变成红色 效果图: 任务 (1)使用通配符选择器 (2)使用jQuery的.css()方法设置样式,语法css('属性 '属性值') 参考代码 ...
- 基于ASP.Net Core开发一套通用后台框架记录-(数据库设计(权限模块))
写在前面 本系列博客是本人在学习的过程中搭建学习的记录,如果对你有所帮助那再好不过.如果您有发现错误,请告知我,我会第一时间修改. 前期我不会公开源码,我想是一点点敲代码,不然复制.粘贴那就没意思了. ...
- LOJ#503. 「LibreOJ β Round」ZQC 的课堂(容斥+FHQTreap)
题面 传送门 题解 首先\(x\)和\(y\)两维互相独立,可以分开考虑,我们以\(x\)为例 我们把\(x\)做个前缀和,那么就是问有多少\(i\)满足\(s_is_{i-1}<0\),其中\ ...
- c语言 error C4996: 'strupr': The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name
问题: 在使用visual studio 2013,进行调试执行代码时,出现如下错误: error C4996: 'strupr': The POSIX name for this item is d ...
- 贪心 Codeforces Round #135 (Div. 2) C. Color Stripe
题目传送门 /* 贪心:当m == 2时,结果肯定是ABABAB或BABABA,取最小改变量:当m > 2时,当与前一个相等时, 改变一个字母 同时不和下一个相等就是最优的解法 */ #incl ...
- 398 Random Pick Index 随机数索引
给定一个可能含有重复元素的整数数组,要求随机输出给定的数字的索引. 您可以假设给定的数字一定存在于数组中.注意:数组大小可能非常大. 使用太多额外空间的解决方案将不会通过测试.示例:int[] num ...
- 【转】Spark:Master High Availability(HA)高可用配置的2种实现
原博文出自于: 感谢! Spark Standalone集群是Master-Slaves架构的集群模式,和大部分的Master-Slaves结构集群一样,存在着Master单点故障的问题.如何解决这个 ...
- 构建一个.net的干货类库,以便于快速的开发 - 工具类
相信每一个开发的框架都会有一个工具类,工具类的作用有很多,通常我会将最常用的方法放在工具类里 取得用户IP 取得网站根目录的物理路径 枚举相关 非法关键字检查 绝对路径改为相对路径 获取小数位(四舍五 ...
- Kerberos 简介——教你做个好人
文章导读: 对称加密 非对称加密 数字证书 Kerberos认证流程 Hadoop生态利用Kerberos认证机制来识别可靠的服务和节点,保障Hadoop集群的安全,那么Kerberos到底是什么?为 ...
- 继续C#开发or转做产品
本人今年大四,C#开发,在一家公司实习了一年后,面临一个选择,是继续C#开发还是转做产品?我C#开发能力目前还一般,但很有兴趣.也喜欢设计与创意,做过产品专员.求大婶们指导!