练习链接:http://acm.njupt.edu.cn/vjudge/contest/view.action?cid=171#overview
I题 Find The Multiple
Time Limit: 1000MS | Memory Limit: 10000K | |||
Total Submissions: 18823 | Accepted: 7618 | Special Judge |
Description
Input
Output
Sample Input
2
6
19
0
Sample Output
10
100100100100100100
111111111111111111
Source
#include
#define ll long long
ll n, ans;
bool find;
void DFS(ll num, int deep)
{
if(deep > 18 || find)
return;
if(num % n == 0)
{
find = true;
ans = num;
return;
}
DFS(num * 10, deep + 1);
DFS(num * 10 + 1, deep + 1);
}
int main()
{
//for(int i = 1; i < 201; i++)
while(scanf("%lld", &n) != EOF && n)
{
// n = i;
ans = 0;
find = false;
DFS(1, 0);
printf("%lld\n", ans);
}
}
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11991 | Accepted: 6809 |
Description
1033The cost of this solution is 6 pounds. Note that the digit 1 which got pasted over in step 2 can not be reused in the last step – a new 1 must be purchased.
1733
3733
3739
3779
8779
8179
Input
Output
Sample Input
3
1033 8179
1373 8017
1033 1033
Sample Output
6
7
0
Source
#include
#include
#include
using namespace std;
int st, ed;
int prime[1000001];
bool used[1000001];
struct NUM
{
int num;
int step;
};
void get_prime() //素数筛
{
memset(prime, 1, sizeof(prime));
prime[1] = 0;
for(int i = 2; i <= 1000; i++)
if(prime[i])
for(int j = i * i; j <= 1000000; j += i)
prime[j] = 0;
}
int BFS()
{
memset(used, false, sizeof(used));
queue q;
NUM s, tmp, cur;
s.num = st;
s.step = 0;
used[st] = true;
q.push(s);
while(!q.empty())
{
cur = q.front();
q.pop();
if(cur.num == ed)
return cur.step;
tmp = cur;
tmp.num -= (tmp.num % 10);
for(int i = 0; i < 10; i++) //拆个位
{
tmp.num += i;
tmp.step++;
if(tmp.num == ed)
return tmp.step;
//若该数字没使用过且是个素数则标记为已使用并进队列
if(!used[tmp.num] && prime[tmp.num])
{
used[tmp.num] = true;
q.push(tmp);
}
tmp.num -= i; //还原
tmp.step--;
}
tmp = cur;
tmp.num -= (((tmp.num / 10) % 10) * 10);
for(int i = 0; i < 10; i++) 拆十位
{
tmp.num += (i * 10);
tmp.step++;
if(tmp.num == ed)
return tmp.step;
if(!used[tmp.num] && prime[tmp.num])
{
used[tmp.num] = true;
q.push(tmp);
}
tmp.num -= (i * 10);
tmp.step--;
}
tmp = cur;
tmp.num -= (((tmp.num / 100) % 10) * 100);
for(int i = 0; i < 10; i++) //拆百位
{
tmp.num += (i * 100);
tmp.step++;
if(tmp.num == ed)
return tmp.step;
if(!used[tmp.num] && prime[tmp.num])
{
used[tmp.num] = true;
q.push(tmp);
}
tmp.num -= (i * 100);
tmp.step--;
}
tmp = cur;
tmp.num -= ((tmp.num / 1000) * 1000);
for(int i = 1; i < 10; i++) //拆千位
//!!!千位的第一位不能是0!
{
tmp.num += (i * 1000);
tmp.step++;
if(tmp.num == ed)
return tmp.step;
if(!used[tmp.num] && prime[tmp.num])
{
used[tmp.num] = true;
q.push(tmp);
}
tmp.num -= (i * 1000);
tmp.step--;
}
}
return -1;
}
int main()
{
int n, ans;
scanf("%d", &n);
get_prime();
while(n --)
{
scanf("%d %d", &st, &ed);
ans = BFS();
if(ans == -1)
printf("Impossible\n");
else
printf("%d\n", ans);
}
}
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 10300 | Accepted: 4362 | Special Judge |
Description
You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:
Write a program to find the shortest possible sequence of these operations that will yield exactlyC liters of water in one of the pots.
Input
On the first and only line are the numbers A, B, and C. These are all integers in the range from 1 to 100 andC≤max(A,B).
Output
The first line of the output must contain the length of the sequence of operationsK. The followingK lines must each describe one operation. If there are several sequences of minimal length, output any one of them. If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.
Sample Input
3 5 4
Sample Output
6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)
Source
#include
#include
#include
using namespace std;
int A, B, C, ans;
int re[100000], cnt = 0;
bool vis[105][105];
char out[7][10] = {"","FILL(1)", "FILL(2)", "DROP(1)", "DROP(2)", "POUR(1,2)", "POUR(2,1)"};
struct NUM
{
int a, b; //a, b表示两个杯子中水的体积
int step; //step表示操作次数
int now; //now表示当前的操作序号
int pre[5000][10]; //pre记录顺序
};
void BFS()
{
memset(vis, false, sizeof(vis));
queue q;
NUM st, t, cur;
memset(st.pre, 0, sizeof(st.pre));
st.a = 0;
st.b = 0;
st.step = 0;
st.now = 0;
q.push(st);
while(!q.empty())
{
t = q.front();
cur = t;
q.pop();
if(t.a == C || t.b == C)
{
printf("%d\n", t.step);
while(t.now) //迭代得到操作序列
{
re[cnt++] = t.now;
t.now = t.pre[t.step--][t.now];
}
return;
}
for(int i = 1; i <= 6; i++)
{
t = cur; //每次要将t还原!
if(i == 1)//fill(1)
{
t.now = 1;
t.a = A;
t.step ++;
if(!vis[t.a][t.b])
{
vis[t.a][t.b] = true;
t.pre[t.step][t.now] = cur.now;
q.push(t);
}
}
if(i == 2) //fill(2)
{
t.now = 2;
t.b = B;
t.step ++;
if(!vis[t.a][t.b])
{
vis[t.a][t.b] = true;
t.pre[t.step][t.now] = cur.now;
q.push(t);
}
}
if(i == 3) // drop(1)
{
t.now = 3;
t.a = 0;
t.step ++;
if(!vis[t.a][t.b])
{
vis[t.a][t.b] = true;
t.pre[t.step][t.now] = cur.now;
q.push(t);
}
}
if(i == 4)//drop(2)
{
t.now = 4;
t.b = 0;
t.step ++;
if(!vis[t.a][t.b])
{
vis[t.a][t.b] = true;
t.pre[t.step][t.now] = cur.now;
q.push(t);
}
}
if(i == 5)//pour(1,2)
{
t.now = 5;
int tmp = t.b;
if(t.b + t.a >= B)
{
t.b = B;
t.a -= (B - tmp);
}
else
{
t.b += t.a;
t.a = 0;
}
t.step ++;
if(!vis[t.a][t.b])
{
vis[t.a][t.b] = true;
t.pre[t.step][t.now] = cur.now;
q.push(t);
}
}
if(i == 6) //pour(2,1)
{
t.now = 6;
int tmp = t.a;
if(t.b + t.a >= A)
{
t.a = A;
t.b -= (A - tmp);
}
else
{
t.a += t.b;
t.b = 0;
}
t.step ++;
if(!vis[t.a][t.b])
{
vis[t.a][t.b] = true;
t.pre[t.step][t.now] = cur.now;
q.push(t);
}
}
}
}
ans = -1;
return;
}
int main()
{
scanf("%d %d %d", &A, &B, &C);
BFS();
if(ans == -1)
printf("impossible\n");
else
for(int i = cnt - 1; i >= 0; i--)
printf("%s\n", out[re[i]]);
}
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 17406 | Accepted: 6769 |
Description
Input
Output
Escaped in x minute(s).
Trapped!
Sample Input
3 4 5
S....
.###.
.##..
###.#
#####
#####
##.##
##...
#####
#####
#.###
####E
1 3 3
S##
#E#
###
0 0 0
Sample Output
Escaped in 11 minute(s).
Trapped!
Source
#include
#include
#include
using namespace std;
int const MAX = 35;
int d[MAX][MAX][MAX];
int vis[MAX][MAX][MAX];
int l, r, c;
int sx, sy, sz, ex, ey, ez;
int dx[] = {0,0,0,0,1,-1};
int dy[] = {0,0,1,-1,0,0};
int dz[] = {1,-1,0,0,0,0};
struct Node
{
int x, y, z;
int step;
};
int BFS()
{
memset(vis, 0, sizeof(vis));
queue q;
Node st, t;
st.x = sx;
st.y = sy;
st.z = sz;
vis[sx][sy][sz] = 1;
st.step = 0;
q.push(st);
while(!q.empty())
{
st = q.front();
q.pop();
for(int i = 0; i < 6; i++)
{
t = st;
t.x += dx[i];
t.y += dy[i];
t.z += dz[i];
t.step++;
if(t.x == ex && t.y == ey && t.z == ez)
return t.step;
if(t.x >= l || t.y >= r || t.z >= c || t.x < 0 || t.y < 0 || t.z < 0)
continue;
if(d[t.x][t.y][t.z] == 0 || vis[t.x][t.y][t.z])
continue;
vis[t.x][t.y][t.z] = 1;
q.push(t);
}
}
return -1;
}
int main()
{
char s[35];
int ans;
while(scanf("%d %d %d", &l, &r, &c) != EOF && (l + r + c))
{
memset(d, 1, sizeof(d));
ans = -1;
for(int i = 0; i < l; i++)
{
for(int j = 0; j < r; j++)
{
scanf("%s", s);
for(int k = 0; k < c; k++)
{
if(s[k] == '#')
d[i][j][k] = 0;
if(s[k] == 'S')
{
sx = i;
sy = j;
sz = k;
}
if(s[k] == 'E')
{
ex = i;
ey = j;
ez = k;
}
}
}
}
ans = BFS();
if(ans == -1)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n", ans);
}
}
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 16353 | Accepted: 4635 |
Description
Input
Output
Sample Input
1 2 3 0 ; three different stamp types
7 4 0 ; two customers
1 1 0 ; a new set of stamps (two of the same type)
6 2 3 0 ; three customers
Sample Output
7 (3): 1 1 2 3
4 (2): 1 3
6 ---- none
2 (2): 1 1
3 (2): tie
Source
#include
#include
using namespace std;
//不同种类的面值,先前最佳解,当前解
int kind[100], pre[4], cur[4];
//种类,需求,先前已存在最佳解的种类数、张数、单张最大面值
int type, need, pKind, pNum, pVal;
//最佳方案数
int ans;
//当前可以购买的种类,上次购买的种类,当前购买的种类数、张数、单张最大面值
void DFS(int k, int lKind, int cKind, int cNum, int cVal, int cost)
{
if(cNum > 4 || (cNum == 4 && cost != need))
return;
if(cost == need) //可行方案
{
if( (pKind == 0) || //先前没有可行解
(cKind > pKind) || //种类多
(cKind == pKind && cNum < pNum) || //张数少
(cKind == pKind && cNum == pNum && cVal > cVal)) //面值大
{
pKind = cKind;
pNum = cNum;
pVal = cVal;
for(int i = 0; i < cNum; i++)
pre[i] = cur[i];
ans = 1;
}
else if(cKind == pKind && cNum == pNum && cVal == pVal) //存在多种最佳方案
ans ++;
return;
}
for(int i = k; i < type; i++)
{
if(cost + kind[i] > need) //排过序
break;
int tKind = cKind, tVal = cVal;
if(lKind != i)
tKind = cKind + 1;
if(cVal < kind[i])
tVal = kind[i];
cur[cNum] = kind[i];
DFS(i, i, tKind, cNum + 1, tVal, cost + kind[i]);
}
}
int main()
{
while(scanf("%d", &kind[0]) != EOF)
{
type = 0;
while(kind[type] != 0)
scanf("%d", &kind[++type]);
sort(kind, kind + type);
while(scanf("%d", &need) && need)
{
pKind = pNum = pVal = 0;
ans = 1;
DFS(0, -1, 0, 0, 0, 0);
if(pKind == 0)
printf("%d ---- none\n", need);
else if(ans > 1)
printf("%d (%d): tie\n", need, pKind);
else
{
printf("%d (%d): ", need, pKind);
for(int i = 0; i < pNum - 1; i++)
printf("%d ", pre[i]);
printf("%d\n", pre[pNum - 1]);
}
}
}
}