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 ...
随机推荐
- 火狐添加消息头 Modify Header Value (HTTP Headers)
火狐浏览器添加组件 : Modify Header Value (HTTP Headers)
- 关闭linux终端进程
[root@linux-node1 ~]# w 22:16:45 up 24 days, 24 min, 2 users, load average: 0.28, 0.17, 0.15 USER TT ...
- size(A,1)
在matlab中,size()用来返回数据序列的行数和列数.size(A,1)返回的是矩阵A所对应的行数.另外,(1)s=size(A), 当只有一个输出参数时,返回一个行向量,该行向量的第一个元素时 ...
- 【BZOJ 1059】[ZJOI2007]矩阵游戏
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 最后要求对于所有的i,a[i][i]=1 那么,如果第i行的第j列为1. 就说明我们可以把这个第i行换到第j行. 因为这样的话,a[ ...
- 【codeforces 29B】Traffic Lights
[题目链接]:http://codeforces.com/problemset/problem/29/B [题意] 一辆车; 让从A开到B; 然后速度是v; (只有在信号灯前面才能停下来..否则其他时 ...
- openSessionInView的使用原理及性能分析
看到好多项目中用到了openSessionInView,这种做法无非是开发方便,能够在JSP页面中操作数据库层方面的业务. 下边说下openSessionInView的使用方法及性能问题. 使用: 1 ...
- CorePlot学习六---点击scatterPlot中的symbol点时弹出对应的凝视
因为项目须要用到用户点击 symbol时,弹出对应的具体信息,发现国内解说的比較少,经过一番搜索验证最终解决,先看效果图: 详细须要改动的代码例如以下: 首先要引用托付方法:CPTScatterPlo ...
- pyspark.mllib.feature module
Feature Extraction Feature Extraction converts vague features in the raw data into concrete numbers ...
- A string is a sequence
A string is a sequence of characters. You can access the characters one at a time with the bracket o ...
- 15.map映射
#include <iostream> #include <map> #include <cstring> using namespace std; //map常规 ...