hdu1495非常可乐

大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为。因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和seeyou一样多。但seeyou的手中只有两个杯子,它们的容量分别是N 毫升和M 毫升 可乐的体积为S (S<101)毫升 (正好装满一瓶) ,它们三个之间可以相互倒可乐 (都是没有刻度的,且 S==N+M,101>S>0,N>0,M>0) 。聪明的ACMER你们说他们能平分吗?如果能请输出倒可乐的最少的次数,如果不能输出”NO”。
Input
三个整数 : S 可乐的体积 , N 和 M是两个杯子的容量,以”0 0 0”结束
Output
如果能平分的话请输出最少要倒的次数,否则输出”NO”
Sample Input
7 4 3
4 1 3
0 0 0
Sample Output
NO
3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//用bfs模拟6种情况来写的,麻烦到爆炸
#include <iostream>
#include<cstdio>
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
typedef long long ll;
int s,n,m;
struct node{
int s,n,m,step;
};
int vis[105][105][105];
void bfs(){
if(s%2==1){printf("NO\n");return;}
memset(vis,0,sizeof(vis));
node point;
point.s=s, point.n=0, point.m=0, point.step=0;
queue<node>q;
q.push(point);
vis[point.s][point.n][point.m]=1;
while(!q.empty()){
node p=q.front();
q.pop();
node tmp;

if((p.s==p.n&&p.s==s/2)||(p.s==p.m&&p.s==s/2)||(p.n==p.m&&p.n==s/2)){
printf("%d\n",p.step);
return ;
}
// s-->n
if(p.s&&(n>p.n)){
if(p.s>n-p.n){
tmp.s=p.s-(n-p.n);
tmp.n=n;
tmp.m=p.m;
}
else{
tmp.s=0;
tmp.n=p.n+p.s;
tmp.m=p.m;
}
if(!vis[tmp.s][tmp.n][tmp.m]){
vis[tmp.s][tmp.n][tmp.m]=1;
tmp.step=p.step+1;
q.push(tmp);
}
}
//s-->m
if(p.s&&m>p.m){
if(p.s>m-p.m){
tmp.s=p.s-(m-p.m);
tmp.n=p.n;
tmp.m=m;
}
else{
tmp.s=0;
tmp.n=p.n;
tmp.m=p.m+p.s;
}
if(!vis[tmp.s][tmp.n][tmp.m]){
vis[tmp.s][tmp.n][tmp.m]=1;
tmp.step=p.step+1;
q.push(tmp);
}
}
// n-->s
if(p.n&&s>p.s){
if(p.n>s-p.s){
tmp.s=s;
tmp.n=p.n-(s-p.s);
tmp.m=p.m;
}
else{
tmp.s=p.s+p.n;
tmp.n=0;
tmp.m=p.m;
}
if(!vis[tmp.s][tmp.n][tmp.m]){
vis[tmp.s][tmp.n][tmp.m]=1;
tmp.step=p.step+1;
q.push(tmp);
}
}
// n-->m
if(p.n&&m>p.m){
if(p.n>m-p.m){
tmp.s=p.s;
tmp.n=p.n-(m-p.m);
tmp.m=m;
}
else{
tmp.s=p.s;
tmp.n=0;
tmp.m=p.m+p.n;
}
if(!vis[tmp.s][tmp.n][tmp.m]){
vis[tmp.s][tmp.n][tmp.m]=1;
tmp.step=p.step+1;
q.push(tmp);
}
}
// m-->s
if(p.m&&s>p.s){
if(p.m>s-p.s){
tmp.s=s;
tmp.n=p.n;
tmp.m=p.m-(s-p.s);
}else{
tmp.s=p.s+p.m;
tmp.n=p.n;
tmp.m=0;
}
if(!vis[tmp.s][tmp.n][tmp.m]){
vis[tmp.s][tmp.n][tmp.m]=1;
tmp.step=p.step+1;
q.push(tmp);
}
}
// m-->n
if(p.m&&n>p.n){
if(p.m>n-p.n){
tmp.s=p.s;
tmp.n=n;
tmp.m=p.m-(n-p.n);
}else{
tmp.s=p.s;
tmp.n=p.n+p.m;
tmp.m=0;
}
if(!vis[tmp.s][tmp.n][tmp.m]){
vis[tmp.s][tmp.n][tmp.m]=1;
tmp.step=p.step+1;
q.push(tmp);
}
}
}
printf("NO\n");return;
}
int main(){
while(scanf("%d%d%d",&s,&n,&m)!=EOF){
if(s+n+m==0)break;
bfs();
}
return 0;
}

开始看到就是一个数学思想,结果没有推出来

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include<cstdio>
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
typedef long long ll;
int gcd(int a,int b){
if(a%b==0) return b;
return gcd(b,a%b);
}
int main(){
int s,n,m;
while(scanf("%d%d%d",&s,&n,&m)!=EOF){
if(s+n+m==0)break;

if(s&1){printf("NO\n");continue;}

int d=gcd(n,m);
//if(s%d){printf("NO\n");continue;}
d=gcd(s,d);
if((s/d)&1) {printf("NO\n");continue;}
else {
printf("%d\n",s/d-1);
}
}
return 0;
}

-------------本文结束感谢您的阅读-------------
0%