Gym - 100338E Numbers 贪心
题意:给你n,k问在1-n中能整出k的字典序最小的数。范围1018
思路:比较简单的贪心了,枚举10的幂m,然后加上k-m%k, 更新答案就可以了,数据一定要用unsigned long long,我就在这里挂了几次,查了半天。
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define LL unsigned long long
#define eps 1e-8
#define INF 0x3f3f3f3f
#define MAXN 10005
using namespace std;
LL n, k;
LL m[];
//char s[25], res[25];
vector<char> s, t, res;
int main()
{
#ifdef ONLINE_JUDGE
freopen("numbers.in", "r", stdin);
freopen("numbers.out", "w", stdout);
#endif //ONLINE_JUDGE
m[] = ;
for (int i = ; i <= ; i++){
m[i] = m[i - ] * 10ULL;
}
while (~scanf("%I64d%I64d", &n, &k)){
if (n == && k == ) break;
for (int i = ; i <= ; i++){
LL p = m[i] % k == ? m[i] : m[i] + k - (m[i] % k);
if (p > n) break;
if (p % k != ) continue;
t.clear();
while (p != ){
t.push_back(p % 10ULL + '');
p /= 10ULL;
}
s.clear();
for (int j = t.size() - ; j >= ; j--){
s.push_back(t[j]);
}
if (i == ){
res = s;
continue;
}
bool flag = true;
for (int j = ; j < s.size(); j++){
if (res.size() < j + ){
flag = true;
break;
}
if (s[j] > res[j]){
flag = true;
break;
}
if (s[j] == res[j]) continue;
flag = false;
break;
}
if (flag) continue;
res = s;
}
for (int i = ; i < res.size(); i++){
printf("%c", res[i]);
}
printf("\n");
}
}
Gym - 100338E Numbers 贪心的更多相关文章
- codeforces Gym 100338E Numbers (贪心,实现)
题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...
- Gym 101102J---Divisible Numbers(反推技巧题)
题目链接 http://codeforces.com/gym/101102/problem/J Description standard input/output You are given an a ...
- Codeforces Gym 100803C Shopping 贪心
Shopping 题目连接: http://codeforces.com/gym/100803/attachments Description Your friend will enjoy shopp ...
- Gym 101775B - Scapegoat - [贪心+优先队列]
题目链接:http://codeforces.com/gym/101775/problem/B Aori is very careless so she is always making troubl ...
- E - Third-Party Software - 2 Gym - 102215E (贪心)
Pavel is developing another game. To do that, he again needs functions available in a third-party li ...
- POJ 2325 Persistent Numbers#贪心+高精度除法
(- ̄▽ ̄)-* 这道题涉及高精度除法,模板如下: ]; ];//存储进行高精度除法的数据 bool bignum_div(int x) { ,num=; ;s[i];i++) { num=num*+ ...
- Codeforces.GYM101612E.Equal Numbers(贪心)
题目链接 \(Description\) 给定\(n\)个数,每次可以将任意一个数乘上任意一个正整数. 求\(k\)次操作后,数列中数的种类最少可以是多少.对每个\(0\leq k\leq n\)输出 ...
- Gym 101201I Postman (贪心)
题意:有个邮递员,要送信,每次最多带 m 封信,有 n 个地方要去送,每个地方有x 封要送,每次都到信全送完了,再回去,对于每个地方,可以送多次直到送够 x 封为止. 析:一个很简单的贪心,就是先送最 ...
- Gym - 100989H (贪心)
After the data structures exam, students lined up in the cafeteria to have a drink and chat about ho ...
随机推荐
- Maven配置Spring+SpringMVC+MyBatis(3.2.2)Pom 以及 IntelliJ IDEA 怎样打开依赖视图
Maven配置Spring+SpringMVC+MyBatis(3.2.2)Pom 配置原则: 利用依赖,将所需的jar包加载到project中. 先依赖主要jar包 Spring + Spring ...
- 福利贴——爬取美女图片的Java爬虫小程序代码
自己做的一个Java爬虫小程序 废话不多说.先上图. 目录命名是用标签缩写,假设大家看得不顺眼能够等完成下载后手动改一下,比方像有强迫症的我一样... 这是挂了一个晚上下载的总大小,只是还有非常多由于 ...
- ACdream 1139(Sum-逆元)
J - Sum Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatu ...
- codeforces #313(div 2)
B. Gerald is into Art time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- poj--2385--Apple Catching(状态dp)
Apple Catching Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Submi ...
- java9新特性-20-Javascript引擎升级:Nashorn
1.官方Feature 236: Parser API for Nashorn 292: Implement Selected ECMAScript 6 Features in Nashorn 2.使 ...
- VS自定义开发向导中的vsdir文件的简单说明
作者:朱金灿 来源:http://blog.csdn.net/clever101 VS自定义开发向导中有一个vsdir文件.这个文件指定了在VS中项目的标题.默认工程名等内容.下面对vsdir文件做一 ...
- iOS网络缓存机制
iOS的网络引擎自带缓存机制: 网络请求在经过网络引擎时有过处理(添加了字段),所以用api的网络请求无法获取缓存. [NSURLCache sharedURLCache]
- UI Framework-1: Aura Gesture Recognizer
Gesture Recognizer Gesture Recognizer Overview This document describes the process by which Touch Ev ...
- Chromium Graphics: Compositor Thread Architecture
Compositor Thread Architecture <jamesr, enne, vangelis, nduca> @chromium.org Goals The main re ...