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 ...
随机推荐
- OD~~helloworld
要爆破的C程序源码: #include <stdio.h> int main() { int x; scanf("%d",&x); ) printf(" ...
- Spring boot中使用log4j记录日志
之前在Spring Boot日志管理一文中主要介绍了Spring Boot中默认日志工具(logback)的基本配置内容.对于很多习惯使用log4j的开发者,Spring Boot依然可以很好的支持, ...
- HDU 1214 圆桌会议 (找规律)
题目链接 Problem Description HDU ACM集训队的队员在暑假集训时经常要讨论自己在做题中遇到的问题.每当面临自己解决不了的问题时,他们就会围坐在一张圆形的桌子旁进行交流,经过大家 ...
- eclipse加速/Nginx配置跨域代理
下班时间到啦! --下班都是他们的,而我,还是什么都没有. eclipse加速 去掉包含js文件的包的js验证,否则每次启动都需要进行校验(右击项目->properties) Nginx配置跨域 ...
- unity3d 资源文件从MAX或者MAYA中导出的注意事项
unity3d 资源文件从MAX或者MAYA中导出的注意事项 1.首先,Unity3d 中,导出带动画的资源有2种导出方式可以选择: 1) 导出资源时,只导出一个文件,保留模型,骨骼和所 ...
- Flask 的一个小应用程序
传说这是Flask 的最小应用程序:hello.py from flask import Flask app = Flask(__name__) @app.route('/') def hello_w ...
- python并发编程之gevent协程(四)
协程的含义就不再提,在py2和py3的早期版本中,python协程的主流实现方法是使用gevent模块.由于协程对于操作系统是无感知的,所以其切换需要程序员自己去完成. 系列文章 python并发编程 ...
- java中String的==和equals的区别
首先看代码1: public static void main(String[] args) { List<String> list=new ArrayList<String> ...
- SYN Flood攻击及防御方法 (转)
原文连接:http://blog.csdn.net/bill_lee_sh_cn/article/details/6065704 一.为什么Syn Flood会造成危害 这要从操作系统的TC ...
- leetcode Two Sum II - Input array is sorted <面试常考题>
题目描述 //二分查找的变形 用头尾两个指针进行 面试考察题 class Solution { public: vector<int> twoSum(vector<int> ...