【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 [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
随机推荐
- MogileFS的实现和bug解决
MogileFS的实现 准备三个主机: centos7.1:tracker节点.database节点.storage节点:192.168.213.251 centos7.2.centos7.3:sto ...
- 【WIP_S2】递归
创建: 2018/01/14 递归 定义 自己召唤自己 通用形式 if (基本情况A的处理) { ... return 值A } else if (基本情况B的处理) ...
- sql清空表数据后重新添加数据存储过程
ALTER PROCEDURE [dbo].[sp_add_Jurisdiction] @CTableName varchar(20), --当前要删除.新增的表 @filedkeyValue var ...
- bootstrap的modal弹窗,在多层窗口关闭时只会关闭自窗口,不再关闭父窗口
bootstrap多层modal弹窗时.当子窗口关闭时,所有父窗口一起关闭. 原因是bootstrap在窗口关闭事件委托时,委托给所有窗口. 如源码: this.$element.on('click. ...
- python使用ddt模块对用例执行操作
import time import unittest import ddt from selenium import webdriver TEST_URL = "http://www.ba ...
- VBNET AutoCAD Activex 切换图层为当前图层失效
最近有朋友询问切换图层的代码 com切换图层 <CommandMethod("mycl")> Public Sub MySubLayerChange() Dim Thi ...
- mysql插入二千万的测试数据进行测试,int和datetime比较
时间存储用int和datetime哪个字段更合适,建立下面的两个表 测试环境是内存2个G,一核的虚拟机 CREATE TABLE `test_inttime` ( `id` int(11) NOT N ...
- ACM_Fibonacci数(同余)
Fibonacci数 Time Limit: 2000/1000ms (Java/Others) Problem Description: 斐波那契数列定义如下:f(0)=0,f(1)=1,f(n+2 ...
- [转]How can I get my webapp's base URL in ASP.NET MVC
本文转自:http://stackoverflow.com/questions/1288046/how-can-i-get-my-webapps-base-url-in-asp-net-mvc May ...
- C#学习-EF在三层中使用
1.搭建普通三层 DAL层,BLL层,Model层,Web层: DAL层引用Model层 BLL层引用DAL层和Model层 Web层引用BLL层和Model层 2.实现EF三层的搭建(添加引用,修改 ...