Divisible by Seven CodeForces - 376C (数论)
You have number a, whose decimal representation quite luckily contains digits 1, 6, 8, 9. Rearrange the digits in its decimal representation so that the resulting number will be divisible by 7.
Number a doesn't contain any leading zeroes and contains digits 1, 6, 8, 9 (it also can contain another digits). The resulting number also mustn't contain any leading zeroes.
The first line contains positive integer a in the decimal record. It is guaranteed that the record of number a contains digits: 1, 6, 8, 9. Number a doesn't
contain any leading zeroes. The decimal representation of number a contains at least 4 and at most 106 characters.
Print a number in the decimal notation without leading zeroes — the result of the permutation.
If it is impossible to rearrange the digits of the number a in the required manner, print 0.
1689
1869
18906
18690
//这道题的思路就是(1).如果在没有0的情况下把1689的任意组合放在后四位,而且1689的组合对1,6,8,9都只取一次
//然后把剩下的数放在前面,这些数对7取余只有0~6这几种情况,所以把这些数对7取余之后的余数再与
//1689的任意组合放在一起,看看哪种组合可以对7取余等于0.
//(2).当输入的数中有0的话,计算一共有多少个0,然后再进行1中的过程,不过把0最后输出就行了
#include<queue>
#include<stack>
#include<vector>
#include<math.h>
#include<stdio.h>
#include<numeric>//STL数值算法头文件
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<functional>//模板类头文件
using namespace std; const int INF=1e9+7;
const int maxn=1010000; char str[maxn],a[maxn];
char xsk[10][5]= {"1869","1968","1689","6198","1698","1986","1896"};//对7取余,余数为0~6的组合情况
int s1,s2,s3,s4;
int main()
{
while(~scanf("%s%*c",str))
{
s1=s2=s3=s4=1;
int t=0,sum1=0,sum2=0;
int len=strlen(str);
for(int i=0; i<len; i++)
if(str[i]=='0')
sum1++;
for(int i=0; i<len; i++)
{
if(str[i]=='0') continue;
if(str[i]=='1'&&s1)
{
s1--;
continue;
}
if(str[i]=='6'&&s2)
{
s2--;
continue;
}
if(str[i]=='8'&&s3)
{
s3--;
continue;
}
if(str[i]=='9'&&s4)
{
s4--;
continue;
}
a[t++]=str[i];
sum2=(sum2*10+str[i]-'0')%7;
}
printf("%s",a);
if(sum2==0)printf("%s",xsk[0]);
if(sum2==1)printf("%s",xsk[3]);
if(sum2==2)printf("%s",xsk[6]);
if(sum2==3)printf("%s",xsk[2]);
if(sum2==4)printf("%s",xsk[5]);
if(sum2==5)printf("%s",xsk[1]);
if(sum2==6)printf("%s",xsk[4]);
for(int i=0; i<sum1; i++)
printf("0");
printf("\n");
}
return 0;
}
Divisible by Seven CodeForces - 376C (数论)的更多相关文章
- CodeForces 300C --数论
A - A Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submit Statu ...
- CodeForces 359D (数论+二分+ST算法)
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=47319 题目大意:给定一个序列,要求确定一个子序列,①使得该子序 ...
- Codeforces 264B 数论+DP
题目链接:http://codeforces.com/problemset/problem/264/B 代码: #include<cstdio> #include<iostream& ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem F (Codeforces 831F) - 数论 - 暴力
题目传送门 传送门I 传送门II 传送门III 题目大意 求一个满足$d\sum_{i = 1}^{n} \left \lceil \frac{a_i}{d} \right \rceil - \sum ...
- CodeForces 1202F(数论,整除分块)
题目 CodeForces 1213G 做法 假设有\(P\)个完整的循环块,假设此时答案为\(K\)(实际答案可能有多种),即每块完整块长度为\(K\),则\(P=\left \lfloor \fr ...
- Vasya and Beautiful Arrays CodeForces - 354C (数论,枚举)
Vasya and Beautiful Arrays CodeForces - 354C Vasya's got a birthday coming up and his mom decided to ...
- Neko does Maths CodeForces - 1152C 数论欧几里得
Neko does MathsCodeForces - 1152C 题目大意:给两个正整数a,b,找到一个非负整数k使得,a+k和b+k的最小公倍数最小,如果有多个k使得最小公倍数最小的话,输出最小的 ...
- Codeforces 716C[数论][构造]
/* CF傻逼构造题 某人要经过n回合游戏,初始分值是2,等级为1. 每次有两种操作 1.无条件,分值加上自己的等级数. 2.当目前的数字是完全平方数并且该数字开方以后是等级数加1的整数倍,那么可以将 ...
- Codeforces 376C. Socks
C. Socks time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...
随机推荐
- web项目中的 log4net的配置
最近用log4net,网上查了很多资料,照着网上的配置大多都不管用,可能我还是有什么地方配置的不对.看出来的朋友平指出.下面是我自己亲测的,可以用! 1.web项目中的web.config 配置log ...
- 小程序制作中 一个奇怪的bug
事情是这样的:原一个购物车 合并本地数据和服务器 数据方法如下 ,正常测试没有问题,当每次重新登录,会调用到这个方法,就会莫名其妙的卡主,debug 发现 a1.length =77731508 导致 ...
- 【leetcode 简单】第一题 两数之和
给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], target ...
- scrapy爬虫框架介绍
一 介绍 Scrapy一个开源和协作的框架,其最初是为了页面抓取 (更确切来说, 网络抓取 )所设计的,使用它可以以快速.简单.可扩展的方式从网站中提取所需的数据.但目前Scrapy的用途十分广泛,可 ...
- javascript复习笔记
/* Javascript:用来在页面中编写特效,和HTML.CSS一样都是有浏览器解析 Javascript语言: 一.JS如何运行(javascript,jscript,vbscript,appl ...
- VueJS $refs 在 ElementUI 中遇到的问题
表单验证的时候 $refs 拿不到 暂且是用 $nextTick 解决,具体原因有待研究 假入在 created 中注册时间来验证 validate,那就放在mounted中 或者...注册了 ev ...
- imperva agent 的重新注册
情况是这样 公司搭了一个环境有mysql的数据库并且安装了agent,imperva管理平台上也可以看到agent的注册信息,但是没想到的是有人把我的虚机给还原快照了,而且还没保存..... 这次写个 ...
- 10 - 函数嵌套-作用域-闭包-LEGB-函数销毁
目录 1 函数嵌套 2 作用域 2.1 global关键字 3 闭包 3.1 nonlocal关键字 4 默认值的作用域 5 变量名解析原则LEGB 6 函数的销毁 1 函数嵌套 一个 ...
- linux脚本学习之路-在suse10环境中生存指定大小指定文件名的压缩文件
#!/bin/bash#-------------------------------------------------------------------------------# Name: ...
- 《深入理解Java虚拟机》笔记--第十三章、线程安全与锁优化
先保证并发的正确性,然后在此基础上来实现高效. 线程安全: 当多个线程访问一个对象时,如果不考虑这些线程在运行时环境下的调度和交替执行,也不需要进行额外的同步,或者在调用方进行任何其他的协调操 ...