题面
大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为。因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和seeyou一样多。但seeyou的手中只有两个杯子,它们的容量分别是N 毫升和M 毫升 可乐的体积为S (S<101)毫升 (正好装满一瓶) ,它们三个之间可以相互倒可乐 (都是没有刻度的,且 S==N+M,101>S>0,N>0,M>0) 。聪明的ACMER你们说他们能平分吗?如果能请输出倒可乐的最少的次数,如果不能输出"NO"。
输入
三个整数 : S 可乐的体积 , N 和 M是两个杯子的容量,以"0 0 0"结束。
输出
如果能平分的话请输出最少要倒的次数,否则输出"NO"。
样例输入
样例输出
提示
无
思路
倒水问题,当S为奇数时无解。数论有简便解法
代码
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 147 148
| using namespace std;
#define out(a) (cout<<__LINE__<<" : "#a" = "<<(a)<<endl) typedef long long ll; const int inf = 0x3f3f3f3f; const int N = 1e2+5;
bool vis[N][N][N] = {{{0}}}; int s, n, m, ans = 0;
struct P { int n, m, s; int step; };
bool bfs() { memset(vis, 0, sizeof(vis));
P sp; sp.s = s; sp.n = 0; sp.m = 0; sp.step = 0;
queue<P> q; q.push(sp); vis[sp.s][sp.n][sp.m] = 1;
while(!q.empty()) { P tp = q.front(); q.pop();
if((!tp.n&&tp.m==tp.s) || (!tp.m&&tp.n==tp.s) || (!tp.s&&tp.n==tp.m)) { ans = tp.step; return true; } P np = tp; int t;
if(tp.n) { t = tp.n+tp.m; if(t>m) { np.m = m; } else { np.m = t; } np.n = t-np.m; np.s = tp.s;
if(!vis[np.s][np.n][np.m]) { np.step = tp.step+1; q.push(np); vis[np.s][np.n][np.m] = 1; }
t = tp.n+tp.s; if(t>s) { np.s = s; } else { np.s = t; } np.n = t-np.s; np.m = tp.m;
if(!vis[np.s][np.n][np.m]) { np.step = tp.step+1; q.push(np); vis[np.s][np.n][np.m] = 1; } }
if(tp.m) { t = tp.m+tp.n; if(t>n) { np.n = n; } else { np.n = t; } np.m = t-np.n; np.s = tp.s;
if(!vis[np.s][np.n][np.m]) { np.step = tp.step+1; q.push(np); vis[np.s][np.n][np.m] = 1; }
t = tp.m+tp.s; if(t>s) { np.s = s; } else { np.s = t; } np.m = t-np.s; np.n = tp.n;
if(!vis[np.s][np.n][np.m]) { np.step = tp.step+1; q.push(np); vis[np.s][np.n][np.m] = 1; } }
if(tp.s) { t = tp.s+tp.n; if(t>n) { np.n = n; } else { np.n = t; } np.s = t-np.n; np.m = tp.m;
if(!vis[np.s][np.n][np.m]) { np.step = tp.step+1; q.push(np); vis[np.s][np.n][np.m] = 1; }
t = tp.s+tp.m; if(t>m) { np.m = m; } else { np.m = t; } np.s = t-np.m; np.n = tp.n;
if(!vis[np.s][np.n][np.m]) { np.step = tp.step+1; q.push(np); vis[np.s][np.n][np.m] = 1; } } } return false; }
int main(void) { while(scanf("%d%d%d", &s, &n, &m)==3 && s && n && m) { if(s%2==0 && bfs()) { printf("%d\n", ans); } else { printf("NO\n"); } } return 0; }
|