ztr loves lucky numbers 傻逼的我来了个大模拟
http://acm.hdu.edu.cn/showproblem.php?pid=5676
这题的正解因该是dfs的,但是有18个位,然后我一算,全排列的话,有18!个啊,那不是很大?但是有很多是相同的,因为4477和第一个和第二个数字调转的结果是一样的。
先说说我模拟的方法。
真的很麻烦,不想看的,给几组数据就跑
7
0
8
4500
47
55
44447778
78
47777445
我是贪心模拟前n位,模拟的时候,如果这一位是3,如果我还有4,那么证明比后面的大了,然后后面的直接按4优先输出即可。
还有可能要加位的,就是78这样,要加位。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 1e2 + ;
char str[maxn];
char ans[maxn];
int findnotseven(int begin, int end) {
for (int i = end; i >= begin; --i) {
if (ans[i] != '') return i;
}
return inf;
}
void work() {
for (int i = ; i <= ; ++i) str[i] = '';
scanf("%s", str + );
int lenstr = strlen(str + );
bool big = false;
int lenans = lenstr;
int four;
int seven;
if (lenstr & ) {
big = true;
four = lenstr / + ;
seven = lenstr / + ;
lenstr++;
} else {
four = lenstr / ;
seven = lenstr / ;
}
int tnow = ;
int add = ;
int pos = false;
char what;
for (int i = ; i <= lenstr; ++i) {
if (big) {
if (four) {
ans[++tnow] = '';
four--;
} else if (seven) {
ans[++tnow] = '';
seven--;
} else while();
} else {
if (str[i] > '') {
if (pos) {
ans[pos] = what;
four = ;
seven = ;
for (int k = ; k <= pos; ++k) {
four += ans[k] == '';
seven += ans[k] == '';
}
four = lenstr / - four;
seven = lenstr / - seven;
big = true;
i = pos;
tnow = pos;
} else {
add = ;
big = true;
four = (lenstr + ) / - ;
seven = (lenstr + ) / ;
i = ;
tnow = ;
}
} else {
if (str[i] >= '' && str[i] <= '') {
if (seven) {
ans[++tnow] = '';
seven--;
if (str[i] != '') {
big = true;
four = ;
seven = ;
for (int k = ; k <= i; ++k) {
four += ans[k] == '';
seven += ans[k] == '';
}
four = lenstr / - four;
seven = lenstr / - seven;
//************//
pos = i;
what = '';
}
} else { //也要改
if (pos) {
ans[pos] = what;
four = ;
seven = ;
for (int k = ; k <= pos; ++k) {
four += ans[k] == '';
seven += ans[k] == '';
}
four = lenstr / - four;
seven = lenstr / - seven;
big = true;
i = pos;
tnow = pos;
} else {
add = ;
big = true;
four = (lenstr + ) / - ;
seven = (lenstr + ) / ;
i = ;
tnow = ;
}
}
} else {
if (four) {
ans[++tnow] = '';
four--;
if (str[i] != '') {
big = true;
four = ;
seven = ;
for (int k = ; k <= i; ++k) {
four += ans[k] == '';
seven += ans[k] == '';
}
four = lenstr / - four;
seven = lenstr / - seven;
pos = i;
what = '';
} else {
if (seven) {
pos = i;
what = '';
}
}
} else if (seven) {
ans[++tnow] = '';
seven--;
big = true;
four = ;
seven = ;
for (int k = ; k <= i; ++k) {
four += ans[k] == '';
seven += ans[k] == '';
}
four = lenstr / - four;
seven = lenstr / - seven;
pos = i;
what = '';
pos = i;
what = '';
}
}
}
}
}
while (add--) {
printf("");
}
for (int i = ; i <= tnow; ++i) {
printf("%c", ans[i]);
}
printf("\n");
} int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
int t;
scanf("%d", &t);
while (t--) work();
return ;
}
说一下按位DFS的做法
预处理出所有结果,首先要知道有多少种结果
当数字有18位的时候,9位4,9位7, 那么C(18, 9)的意思就是选9个位置来放四,其他的放7,那么结果就是C(18,9)
所以总答案数只有66196种。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 1e6 + ;
LL ans[maxn];
int lenans;
void dfs(LL now, int num_four, int num_seven) {
if (num_four == && num_seven == ) {
ans[++lenans] = now;
return;
}
if (num_four == ) {
dfs(now * + , num_four, num_seven - );
} else if (num_seven == ) {
dfs(now * + , num_four - , num_seven);
} else {
dfs(now * + , num_four - , num_seven);
dfs(now * + , num_four, num_seven - );
}
}
void init() {
for (int i = ; i <= ; i += ) {
dfs(, i / , i / );
}
cout << lenans << endl;
}
void work() {
LL n;
scanf("%I64d", &n);
if (n > ans[lenans]) { //最大值777777777444444444。1e17内的最大值
printf("44444444447777777777\n");
} else {
// LL tans = lower_bound(ans + 1, ans + 1 + lenans);
// printf("%I64d\n", tans);
int begin = , end = lenans;
while (begin <= end) {
int mid = (begin + end) >> ;
if (ans[mid] >= n) {
end = mid - ;
} else begin = mid + ;
}
printf("%I64d\n", ans[begin]);
}
}
int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
init();
int t;
scanf("%d", &t);
while (t--) work();
return ;
}
可以学习下DFS的做法
ztr loves lucky numbers 傻逼的我来了个大模拟的更多相关文章
- HDU 5676 ztr loves lucky numbers (模拟)
ztr loves lucky numbers 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/I Description ztr ...
- hdu 5676 ztr loves lucky numbers(dfs+离线)
Problem Description ztr loves lucky numbers. Everybody knows that positive integers are lucky if the ...
- hdu 5676 ztr loves lucky numbers 打表+二分
ztr loves lucky numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- hdu-5676 ztr loves lucky numbers(乱搞题)
题目链接: ztr loves lucky numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K ( ...
- hdu 5676 ztr loves lucky numbers
题目链接:hdu 5676 一开始看题还以为和数位dp相关的,后来才发现是搜索题,我手算了下,所有的super lucky number(也就是只含数字4, 7且4, 7的数量相等的数)加起来也不过几 ...
- hdu5676 ztr loves lucky numbers(dfs)
链接 ztrloveslucky numbers 题意 定义幸运数为:只存在4和7且4和7数量相等的数,给出n,求比>=n的最小幸运数 做法 暴力搜出所有长度从2-18的幸运数,因为最多9个4, ...
- HDU 5676 ztr loves lucky numbers【DFS】
题目链接; http://acm.hdu.edu.cn/showproblem.php?pid=5676 题意: 由4和7组成的且4和7出现次数相同的数称为幸运数字,给定n,求不大于n的最大幸运数字. ...
- hdu_5676_ztr loves lucky numbers
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5676 在这%一下安神,用了我没见过的黑科技next_permutation,至少我是今天才知道的 #i ...
- ztr loves lucky numbers--hdu5676(DFS)
http://acm.hdu.edu.cn/showproblem.php?pid=5676 题目大意: 给你一个数 让你找比这数大并且只含4和7 并且4和7的个数一样 枚举从0到10的18次方之 ...
随机推荐
- Redis使用经验之谈
应用场景 保存用户喜欢的商品信息. 类型: Hash, key: usr:${type_id}:${version_id}:${user_id}:${warehouse_id}, field: ${s ...
- js日期和时间戳互换
<script> function js_strto_time(str_time){ var new_str = str_time.replace(/:/g,'-'); new_str = ...
- 限制远程桌面登录IP的方法
转自:http://www.cnblogs.com/vaexi/articles/2106623.html 限制远程桌面登录IP的方法 第一种方法: 1.打开Windows自带的防火墙2.开放允许例外 ...
- 最基本PSO算法的C++实现
按照James Kennedy & Russell Eberhart (1995)的版本,算法过程如下: [x*] = PSO() P = Particle_Initialization(); ...
- luogu 3389 【模板】高斯消元
大概就是对每一行先找到最大的减小误差,然后代入消元 #include<iostream> #include<cstdio> #include<cstring> #i ...
- BZOJ_3566_[SHOI2014]概率充电器_概率+树形DP
BZOJ_3566_[SHOI2014]概率充电器_概率+树形DP Description 著名的电子产品品牌 SHOI 刚刚发布了引领世界潮流的下一代电子产品——概率充电器: “采用全新纳米级加工技 ...
- bzoj 3992 [SDOI2015] 序列统计 —— NTT (循环卷积+快速幂)
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3992 (学习NTT:https://riteme.github.io/blog/2016-8 ...
- AngularJS系统学习之Module(模块)
本文源自:http://blog.csdn.net/woxueliuyun/article/details/50962645 学习之后略有所得, 来此分享.建议看原文. 模块是提供一些特殊服务的功能块 ...
- storyBoard学习教程二(页面跳转)
本篇是接着上一篇 storyBoard 学习教程一 的补充,有过storyBoard 编程经验的伙伴还是不要阅读本篇博客了,我自己认为,太基础太简单了,为了方便别人学习使用,我还是详细的做了这篇教程. ...
- Python学习资源汇总
Python 简明教程 (入门必看) * 在线浏览: http://woodpecker.org.cn/abyteofpython_cn/chinese/ Python Tutorial 简体中文版 ...