【链接】 我是链接,点我呀:)

【题意】

让你把一个包含数字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的更多相关文章

  1. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  2. 【20.23%】【codeforces 740A】Alyona and copybooks

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  3. 【34.88%】【codeforces 569C】Primes or Palindromes?

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. 【codeforces 546D】Soldier and Number Game

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  5. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  6. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  7. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  8. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  9. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

随机推荐

  1. jQuery中contains和has的区别

    jQuery中contains和has的区别 根据不同的内容和属性可以准确定位到需要找的属性 如何根据内容筛选标签?:contains        匹配包含给定的文本元素$("div:co ...

  2. Linux压缩命令(zip/gz/bz2/tar/tar.gz/tar.bz2)

    一.Linux的压缩格式 .zip . gz . bz2    .tar 1..zip格式(Linux和Windows是可以互传的) 压缩命令 语法:zip 文件名.zip 文件名  ------压缩 ...

  3. cURL模拟HTTP请求(支持HTTPS)

    function setHttpRequest($url,$headers,$params=array(),$method="GET") { $ci = curl_init(); ...

  4. redis的bitmap

    BitMap是什么 就是通过一个bit位来表示某个元素对应的值或者状态,其中的key就是对应元素本身.我们知道8个bit可以组成一个Byte,所以bitmap本身会极大的节省储存空间. Redis中的 ...

  5. WIN2012的桌面和开始菜单跑到什么地方去了

    传统开始菜单取消了,你把鼠标指针移动到左下角的边缘,会有一个小浮窗,就是开始,再点开始就能进到一个全屏的层里面,跟手机界面,那个玩意就是新一代的开始菜单,程序菜单,你在开始菜单上,鼠标右键点一下,下面 ...

  6. python批量下载图片

    从数据库拿了一批图片地址,需要一张一张的把图片下载下来,自从有了python,想到能省事就琢磨如何省事. 代码如下: import urllib.requestf=open("E:\999\ ...

  7. 创建http对象

    package test; import java.net.HttpURLConnection;import java.net.URL; import javax.servlet.http.HttpS ...

  8. CF540C Ice Cave

    思路: 搜索. 加回溯会超时,不加回溯就过了,不是很理解. 实现: #include <iostream> #include <cstdio> using namespace ...

  9. sublime 自定义快捷键

    [ { "keys": ["alt+space"], "command": "auto_complete" }, // ...

  10. Dota2团战实力蔑视人类,解剖5只“AI英雄”

    去年,OpenAI 在 DOTA 的 1v1 比赛中战胜了职业玩家 Dendi,而在距离进阶版 OpenAI Five 系统战胜人类业余玩家不过一个月的时间,今天凌晨,它又以 2:1 的战绩再次完成对 ...