【搜索】 HDU 4474 Yet Another Multiple Problem
2015-10-03 20:27
507 查看
点击打开链接
题意 : 求一个最小的n的倍数 ans,使得ans不包括 输入的几个数字 ,不存在输出 -1
思路 :对于 两个数 A , B 存在 A > B 且 A%n = B%n 则 (A*10 + K ) %n = (B*10 + K ) %n 然而 A*10 + K 仍然 大于 B*10 + K
所以就不需要搜 A 了 ,判断 余数 是否出现过 在剪枝
ans 会很大 所以用字符串模拟
类似: HDU
1226
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <bitset>
#include <deque>
#include <set>
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <map>
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 1<<29;
const LL mod = 1e9+7;
const int MAXN = 100100;//点数的最大值
const int MAXM = 520;//边数的最大值
const double pi = acos(-1);
int a[21213],m;
ULL n;
bool vis[22344];
struct node
{
string ans;
int mmm;
};
queue<node>q;
string gao()
{
while(!q.empty()) q.pop();
node fro;
fro.ans="";fro.mmm=0;
q.push(fro);
while(!q.empty())
{
node x=q.front();
q.pop();
for(int i=0;i<=9;i++)
{
if(a[i]) continue;
if(x.mmm==0&&i==0) continue;
fro.mmm=(x.mmm*10+i)%n;
if(vis[fro.mmm]) continue;
vis[fro.mmm]=1;
fro.ans=x.ans+(char)('0'+i);
if(fro.mmm==0)
return fro.ans;
q.push(fro);
}
}
return "-1";
}
int main()
{
int cas=0;
while(scanf("%I64u%d",&n,&m)!=EOF)
{
memset(a,0,sizeof(a));
for(int i=0;i<m;i++)
{
int x;
scanf("%d",&x);
a[x]=1;
}
memset(vis,0,sizeof(vis));
string ans=gao();
printf("Case %d: ",++cas);
cout<<ans<<endl;
}
return 0;
}
题意 : 求一个最小的n的倍数 ans,使得ans不包括 输入的几个数字 ,不存在输出 -1
思路 :对于 两个数 A , B 存在 A > B 且 A%n = B%n 则 (A*10 + K ) %n = (B*10 + K ) %n 然而 A*10 + K 仍然 大于 B*10 + K
所以就不需要搜 A 了 ,判断 余数 是否出现过 在剪枝
ans 会很大 所以用字符串模拟
类似: HDU
1226
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <bitset>
#include <deque>
#include <set>
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <map>
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 1<<29;
const LL mod = 1e9+7;
const int MAXN = 100100;//点数的最大值
const int MAXM = 520;//边数的最大值
const double pi = acos(-1);
int a[21213],m;
ULL n;
bool vis[22344];
struct node
{
string ans;
int mmm;
};
queue<node>q;
string gao()
{
while(!q.empty()) q.pop();
node fro;
fro.ans="";fro.mmm=0;
q.push(fro);
while(!q.empty())
{
node x=q.front();
q.pop();
for(int i=0;i<=9;i++)
{
if(a[i]) continue;
if(x.mmm==0&&i==0) continue;
fro.mmm=(x.mmm*10+i)%n;
if(vis[fro.mmm]) continue;
vis[fro.mmm]=1;
fro.ans=x.ans+(char)('0'+i);
if(fro.mmm==0)
return fro.ans;
q.push(fro);
}
}
return "-1";
}
int main()
{
int cas=0;
while(scanf("%I64u%d",&n,&m)!=EOF)
{
memset(a,0,sizeof(a));
for(int i=0;i<m;i++)
{
int x;
scanf("%d",&x);
a[x]=1;
}
memset(vis,0,sizeof(vis));
string ans=gao();
printf("Case %d: ",++cas);
cout<<ans<<endl;
}
return 0;
}
相关文章推荐
- Ubuntu 14.10下部署Django到Apache服务器_
- 深入理解 [NSBundle mainBundle]
- Redis快速入门:初识Redis
- eclipse 的重构使用
- 这些练习并没有多少用,但舍不得就扔了,作个记录吧。
- 将内存单元中小写字符改成大写字符
- 选择Key-Value Store
- Key-Value存储系统简介
- IOS的app程序中的文件目录的功能和使用详细说明 - 包括itune同步的部分
- HDU 3729 I'm Telling the Truth(2010 Asia Tianjin Regional Contest )
- Hibernate HQL详细说明
- IOS中获取各种文件的目录路径的方法
- Microsoft Visual C++ 6.0快捷键(绝对值得掌握)
- JavaScript设计模式——状态模式
- (一三〇)UITextField的光标操作扩展
- (一三〇)UITextField的光标操作扩展
- 【Java算法】一天24小时中,时针和分针一共重合多少次?
- ADB的使用
- 【BZOJ】【P4292】【PA2015】【Równanie】【题解】【暴力】
- Android---Unable to execute dex: Multiple dex files define Landroid/support/annotation/AnimRes;