ZOJ 3596Digit Number(BFS+DP)
一道比较不错的BFS+DP题目
题意很简单,就是问一个刚好包含m(m<=10)个不同数字的n的最小倍数。
很明显如果直接枚举每一位是什么这样的话显然复杂度是没有上限的,所以需要找到一个状态表示方法:
令F[i][j] 表示已经用了 i (二进制压位表示)用了 i 这些数字,且余数j为的状态,枚举时直接枚举当前位,那么答案明显就是F[m][0]
我这里将状态i, j存在了一维空间里,即 i * 1000 + j表示,实际上用一个结构体存队列里的点,用二维数组标记状态也是可行的。
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf (-((LL)1<<40))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FIN freopen("in.txt", "r", stdin)
#define FOUT freopen("out.txt", "w", stdout)
#define rep(i, a, b) for(int i = a; i <= b; i ++) template<class T> T CMP_MIN(T a, T b) { return a < b; }
template<class T> T CMP_MAX(T a, T b) { return a > b; }
template<class T> T MAX(T a, T b) { return a > b ? a : b; }
template<class T> T MIN(T a, T b) { return a < b ? a : b; }
template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b; } //typedef __int64 LL;
typedef long long LL;
const int MAXN = ;
const int MAXM = ;
const double eps = 1e-;
//LL MOD = 987654321; int t, n, m, x;
struct Node {
bool vis;
char num;
int pre, cnt;
}s[(<<) * ];
char ans[], d[]; int bfs()
{
queue<int>q;
q.push();
while(!q.empty()) {
int head = q.front();q.pop();
rep (i, , ) {
if(head == && i == ) continue;
int mod = (head % * + i) % n;
int tail = ((head / ) | ( << i)) * + mod;
if(s[tail].vis)
continue;
s[tail].vis = true;
s[tail].num = i + '';
s[tail].pre = head;
s[tail].cnt = s[head].cnt + ((head / ) & ( << i) ? : );
if(s[tail].cnt == m && mod == ) {
return tail;
}
if(s[tail].cnt <= m) q.push(tail);
}
}
return ;
} //calc a / b
char* divide(char *a, int len, int b) {
mem0(d);
int i = , cur = , l = ;
while(cur < b && i < len) {
cur = cur * + a[i++] - '';
}
d[l++] = cur / b + '';
while(i < len) {
cur = cur % b * + a[i++] - '';
d[l++] = cur / b + '';
}
return d;
} void print(int ed) {
int len = ;
mem0(ans);
while(ed) {
ans[len++] = s[ed].num;
ed = s[ed].pre;
}
reverse(ans, ans + len);
printf("%s=%d*%s\n", ans, n, divide(ans, len, n));
} int main()
{
//FIN;
while(cin >> t) while(t--) {
cin >> n >> m;
mem0(s);
if( !(x = bfs()) ) {
puts("Impossible");
}
else {
print(x);
}
}
return ;
}
ZOJ 3596Digit Number(BFS+DP)的更多相关文章
- HDU 3565 Bi-peak Number(数位DP)题解
题意:我们定义每一位先严格递增(第一位不为0)后严格递减的数为峰(比如1231),一个数由两个峰组成称为双峰,一个双峰的价值为每一位位数和,问L~R双峰最大价值 思路:数位DP.显然这个问题和pos有 ...
- 【2019.8.14 慈溪模拟赛 T1】我不是!我没有!别瞎说啊!(notme)(BFS+DP)
\(IDA^*\) 说实话,这道题我一开始没想出正解,于是写了一个\(IDA^*\)... 但神奇的是,这个\(IDA^*\)居然连字符串长度分别为\(2500,4000\)的数据都跑得飞快,不过数据 ...
- codeforces 295C Greg and Friends(BFS+DP)
One day Greg and his friends were walking in the forest. Overall there were n people walking, includ ...
- 【HDU 3709】 Balanced Number (数位DP)
Balanced Number Problem Description A balanced number is a non-negative integer that can be balanced ...
- HDU 5898:odd-even number(数位DP)
http://acm.hdu.edu.cn/showproblem.php?pid=5898 题意:给出一个区间[l, r],问其中数位中连续的奇数长度为偶数并且连续的偶数长度为奇数的个数.(1< ...
- [HDOJ3709]Balanced Number(数位dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3709 题意:求区间[L,R]内每一个数中是否存在一位,使得左边的各位数*距离=右边的各位数*距离(自己 ...
- ZOJ 3689 Digging(贪心+dp)
Digging Time Limit: 2 Seconds Memory Limit: 65536 KB When it comes to the Maya Civilization, we ...
- Codeforces Gym101201B:Buggy Robot(BFS + DP)
题目链接 题意 给出一个n*m的地图,还有一个操作序列,你原本是要按照序列执行操作的,但是你可以修改操作:删除某些操作或者增加某些操作,问从'R'到'E'最少需要多少次修改操作. 思路 和上次比赛做的 ...
- HDU3709 Balanced Number (数位dp)
Balanced Number Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Descript ...
随机推荐
- setTimeOut() 和 setTimeInterval()
setTimeOut()は.指定された時間「待ってから」指定された動作を行う関数です.setTimeOut():等待指定时间,执行指定方法. setTimeInterval()は.指定された時間「間隔 ...
- 【经验】Windows7、8、8.1 MSI安装错误Error Code 2502 & 2503 解决方法
[因] 今天升级TortoiseSVN到1.8.8,出现问题:Error Code 2502 & 2503,一直不能安装成功. 上网一搜,国内没找到好的解决方法,在一个外文网上找到了方案,原链 ...
- [转]C#中的?和??
原文链接:http://msdn.microsoft.com/zh-tw/library/2cf62fcy%28VS.80%29.aspx 使用可為 Null 的型別 (C# 程式設計手冊) Visu ...
- codevs 1138 聪明的质监员
二分+前缀和. #include<iostream> #include<cstdio> #include<cstring> #include<cmath> ...
- JAVA规则引擎 -- Drools
Drools是一个基于java的规则引擎,开源的,可以将复杂多变的规则从硬编码中解放出来,以规则脚本的形式存放在文件中,使得规则的变更不需要修正代码重启机器就可以立即在线上环境生效. 本文所使用的de ...
- 【基础数学】质数,约数,分解质因数,GCD,LCM
1.质数: 质数(prime number)又称素数,有无限个.一个大于1的自然数,除了1和它本身外,不能整除以其他自然数(质数),换句话说就是该数除了1和它本身以外不再有其他的因数. 2.约数: 如 ...
- 【转】U-boot分析与移植(1)----bootloader分析
原文网址:http://blog.csdn.net/jianchi88/article/details/7061089 一.Boot Loader 概念 就是在操作系统内核运行之前运行的一段小程序. ...
- phonegap 使用极光推送实现消息推送
最近一直在研究各种推送,ios的由于是apns,比较容易实现,但是andriod的就比较麻烦.后来看了很多解决方案,gcm明显是不行的,其他的方案更是一头雾水,而且需要做第二次开发,太麻烦,后来就选择 ...
- IT技术人士 不能一辈子靠技术生存
我现在是自己做,但我此前有多年在从事软件开发工作,当回过头来想一想自己,觉得特别想对那些初学JAVA/DOT.NET技术的朋友说点心里话,希望你们能从我们的体会中,多少受点启发(也许我说的不好,你不赞 ...
- hdu 1115(计算多边形重心)
题意:已知一多边形没有边相交,质量分布均匀.顺序给出多边形的顶点坐标,求其重心. 分析: 求多边形重心的题目大致有这么几种: 1,质量集中在顶点上.n个顶点坐标为(xi,yi),质量为mi,则重心 X ...