又要开始一段搜索的路程了。

最近看了这题,在网上看到一个结论,任何一个数倍数都能被不超过两个数字组成,假如一个数n个A%x=b,那么必然有m个A%=b那么此时n个A减去m个B就能够被x整除,那么此时就有了上述的结论,在配合上余数来进行一个搜索,就是把余下的数字作为一个状态来进行广搜。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
using std::queue;
int const N = ;
int const M = ;
int cnt[M],father[M],record[M];
char ans[M],tmp[M];
int n,k,a1,t1,e[N],c;
bool bfs()
{
queue<int> q;
while(!q.empty())q.pop();
for(int i=;i<c;i++)
{
if(!e[i]||cnt[e[i]%n])continue;
cnt[e[i]%n]=;
father[e[i]%n]=-;
record[e[i]%n]=e[i];
q.push(e[i]%n);
}
while(!q.empty())
{
int num=q.front();
q.pop();
if(a1>&&cnt[num]>a1)return false;
if(num==)return true;
for(int i=;i<c;i++)
{
int nt=(num*k+e[i])%n;
if(!cnt[nt])
{
cnt[nt]=cnt[num]+;
father[nt]=num;
record[nt]=e[i];
q.push(nt);
}
}
}
return false;
}
bool cmp()
{
if(a1==)return true;
if(t1>a1)return false;
if(t1<a1)return true;
for(int i=;i<t1;i++)
{
if(ans[i]>tmp[i])return true;
if(ans[i]<tmp[i])return false;
}
return false;
}
void over()
{
for(int i=;i<t1;i++)
ans[i]=tmp[i];
ans[t1]='\0';
a1=t1;
}
void display(int i)
{
tmp[cnt[i]-]=record[i]+'';
if(father[i]!=-)
display(father[i]);
}
int main()
{
while(scanf("%d %d",&n,&k)!=EOF)
{
a1=;
for(int i=;i<k;i++)
{
memset(cnt,,sizeof(cnt));
e[]=i,c=;
if(bfs())
{
display();
t1=cnt[];
if(cmp())over();
}
}
if(a1==)
for(int i=;i<k;i++)
{
for(int j=;j<i;j++)
{
memset(cnt,,sizeof(cnt));
e[]=j,e[]=i,c=;
if(bfs())
{
display();
t1=cnt[];
if(cmp())over();
}
}
}
for(int i=;i<a1;i++)printf("%c",ans[i]);
printf("\n");
}
return ;
}

hdu 4294 数学分析+搜索的更多相关文章

  1. HDU 4294 Multiple(搜索+数学)

    题意: 给定一个n,让求一个M,它是n个倍数并且在k进制之下 M的不同的数字最少. 思路: 这里用到一个结论就是任意两个数可以组成任何数的倍数.知道这个之后就可以用搜索来做了.还有一个问题就是最多找n ...

  2. hdu 4294(bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4294 思路:题目的意思是说,给你n,k,则求出n的正整数倍数,使得这个数字在k进制下表示的时候需要的不 ...

  3. hdu 5468(莫比乌斯+搜索)

    hdu 5468 Puzzled Elena   /*快速通道*/ Sample Input 5 1 2 1 3 2 4 2 5 6 2 3 4 5   Sample Output Case #1: ...

  4. HDU 4499.Cannon 搜索

    Cannon Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Subm ...

  5. HDU 1045 (DFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1045 题目大意:在不是X的地方放O,所有O在没有隔板情况下不能对视(横行和数列),问最多可以放多少个 ...

  6. HDU 1180 (BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1180 题目大意:迷宫中有一堆楼梯,楼梯横竖变化.这些楼梯在奇数时间会变成相反状态,通过楼梯会顺便到达 ...

  7. HDU 2531 (BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2531 题目大意: 你的身体占据多个点.每次移动全部的点,不能撞到障碍点,问撞到目标点块(多个点)的最 ...

  8. HDU 1026 (BFS搜索+优先队列+记录方案)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 题目大意:最短时间内出迷宫.迷宫里要杀怪,每个怪有一定HP,也就是说要耗一定时.输出方案. 解 ...

  9. HDU 1312 (BFS搜索模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1312 题目大意:问迷宫中有多少个点被访问. 解题思路: DFS肯定能水过去的.这里就拍了一下BFS. ...

随机推荐

  1. const char*、char*、char* const、char[]、string的区别

    1.const char* p: p is a pointer to const char(char const* p 一样)   意思就是不能通过p指针来修改p指向的内容(但是内容可以修改). 2. ...

  2. OpenJudge/Poj 1844 Sum

    1.链接地址: http://bailian.openjudge.cn/practice/1844 http://poj.org/problem?id=1844 2.题目: Sum Time Limi ...

  3. 安卓热更新之Nuwa实现步骤

    安卓热更新之Nuwa实现步骤 最近热更新热修复的功能在安卓应用上越发火热,终于我的产品也提出了相应的需求. 经过两天的研究,搞定了这个功能,在这里还要多谢大神们的博客,大神们的原理分析很到位,不过对于 ...

  4. Weui 微信网站开发样式插件使用教程

    微信的网页样式正式发布了,搜了一下,正式引入了乐学一百微信端的项目中. <div class="weui_grids"> <a href="javasc ...

  5. Raphael:JS矢量图形库

    Raphael:JS矢量图形库 2016-08-29 http://dmitrybaranovskiy.github.io/raphael/

  6. memcached全面剖析--2

    理解memcached的内存存储 下面是<memcached全面剖析>的第二部分. 发表日:2008/7/9 作者:前坂徹(Toru Maesaka) 原文链接:http://gihyo. ...

  7. 2014年度辛星html教程夏季版第六节

    不知道这样大家会不会感觉枯燥,总之不管大家怎么样,辛星始终会陪伴大家的,期待您的关注和支持,也衷心的希望我的博客能让您少走弯路,获得更系统的知识. ***************表格********* ...

  8. MVC-EditorFor与TextBoxFor的区别

    EditorFor会根据后面提供的数据类型自动判断生成的控件类型(比如TextBox,CheckBox等): TextBoxFor生成的只是一个TextBox.

  9. jquery 插件页面回到顶部

    引用: jquery.scrollUp.min.js js: $.scrollUp({ scrollName: 'scrollUp', // Element ID topDistance: '300' ...

  10. StreamCQL

    StreamCQLhttps://github.com/HuaweiBigData/StreamCQL http://blog.csdn.net/viewcode/article/details/90 ...