HDU_1495_非常可乐

[url]http://acm.hdu.edu.cn/showproblem.php?pid=1495[/url]

[size=medium]Problem Description
大家一定觉的运动以后喝可乐是一件很惬意的事情,但是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[/size]

[color=blue][size=medium]因为每次倒的方法只有6种,所以可以利用广搜枚举所有方法[/size][/color]


#include <iostream>
#include <queue>
using namespace std;

struct cola{
int x, y, z, num; //x,y,z 表示当前杯子中的含量
}front, temp;
int s, n, m; //s,n,m 分别是装[x,y,z]的杯子的容积
bool hash[105][105][105]; //hash记忆到达状态,第二次出现,死循环,说明已经不可能

void bfs () //6种情况,分别独立对待,可倒成功者都入队列
{
queue<cola> q;
q.push (front);
while (!q.empty())
{
front = q.front(), q.pop();
if ((front.x == 0 && front.y == front.z) ||
(front.y == 0 && front.x == front.z) ||
(front.z == 0 && front.x == front.y))
{
printf ("%d\n", front.num); //符合平分情况,跳出
return ;
}
temp = front; //每步独立进行,所以都需要初始化
temp.num = front.num + 1;
//x -> y ①, x倒给y 【给出此步分析,下面同理】
if (temp.x > 0 && temp.y < n)
{
int left = n - temp.y; //求出容积是n的杯子还剩多少体积没装
if (temp.x >= left) //装x的杯子中的含量x>left, 即不可以全部倒入y那个杯子, 倒完后x那个杯子倒去left, y就满了--达到最大容积n
temp.x -= left, temp.y = n;
else temp.y += temp.x, temp.x = 0; //反之可以全部倒入
//cout << "-" << temp.x << ' ' << temp.y << ' ' << temp.z << endl;
if (!hash[temp.x][temp.y][temp.z])
{ //记录出现的3个杯子当前状态,如果再次出现,肯定是死循环,所以必无答案
hash[temp.x][temp.y][temp.z] = true;
q.push (temp);
}
}
temp = front; //初始化,下面同理
temp.num = front.num + 1;
//x -> z ②
if (temp.x > 0 && temp.z < m)
{
int left = m - temp.z;
if (temp.x >= left)
temp.x -= left, temp.z = m;
else temp.z += temp.x, temp.x = 0;
//cout << "--" << temp.x << ' ' << temp.y << ' ' << temp.z << endl;
if (!hash[temp.x][temp.y][temp.z])
{
hash[temp.x][temp.y][temp.z] = true;
q.push (temp);
}
}
temp = front;
temp.num = front.num + 1;
//y -> x ③
if (temp.y > 0 && temp.x < s)
{
int left = s - temp.x;
if (temp.y >= left)
temp.y -= left, temp.x = s;
else temp.x += temp.y, temp.y = 0;
if (!hash[temp.x][temp.y][temp.z])
{
hash[temp.x][temp.y][temp.z] = true;
q.push (temp);
}
}
temp = front;
temp.num = front.num + 1;
//y -> z ④
if (temp.y > 0 && temp.z < m)
{
int left = m - temp.z;
if (temp.y >= left)
temp.y -= left, temp.z = m;
else temp.z += temp.y, temp.y = 0;
if (!hash[temp.x][temp.y][temp.z])
{
hash[temp.x][temp.y][temp.z] = true;
q.push (temp);
}
}
temp = front;
temp.num = front.num + 1;
//z -> x ⑤
if (temp.z > 0 && temp.x < s)
{
int left = s - temp.x;
if (temp.z >= left)
temp.z -= left, temp.x = s;
else temp.x += temp.z, temp.z = 0;
if (!hash[temp.x][temp.y][temp.z])
{
hash[temp.x][temp.y][temp.z] = true;
q.push (temp);
}
}
temp = front;
temp.num = front.num + 1;
//z -> y ⑥
if (temp.z > 0 && temp.y < n)
{
int left = n - temp.y;
if (temp.z >= left)
temp.z -= left, temp.y = n;
else temp.y += temp.z, temp.z = 0;
if (!hash[temp.x][temp.y][temp.z])
{
hash[temp.x][temp.y][temp.z] = true;
q.push (temp);
}
}
}
puts ("NO"); //还未跳出,说明不可平分
return ;
}
int main()
{
while (scanf ("%d%d%d", &s, &n, &m) != EOF)
{
if (!s && !n && !m)
break;
if (s % 2)
{
puts ("NO");
continue;
}
memset (hash, false, sizeof(hash));
hash[s][0][0] = true;
front.x = s, front.y = 0, front.z = 0, front.num = 0;
bfs ();
}
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,关于 HDU4992 求所有原根的问题,我可以给出以下解答: 1. 首先,我们需要知道什么是原根。原根是指模 n 意义下,所有与 n 互质的数 a 的最小正整数 k 次幂对 n 取模的值覆盖了所有与 n 互质的数。简单来说,就是如果一个数 a 是模 n 意义下的原根,那么 a 的任何正整数次幂对 n 取模的值都不相同,且能覆盖所有与 n 互质的数。 2. 为了求模 n 意义下的所有原根,我们需要先求出与 n 互质的数的个数 phi(n)。phi(n) 可以使用欧拉函数求出。 3. 然后,我们需要枚举模 n 意义下的所有数,判断它是否是原根。具体来说,对于每个 a,我们需要判断 a 的每个小于 phi(n) 的正整数次幂对 n 取模的值是否都不相同,且能覆盖所有与 n 互质的数。如果是,那么 a 就是模 n 意义下的原根。 4. 代码实现可以参考以下 Java 代码: ``` import java.util.*; public class Main { static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } static int phi(int n) { int res = n; for (int i = 2; i * i <= n; i++) { if (n % i == 0) { res = res / i * (i - 1); while (n % i == 0) { n /= i; } } } if (n > 1) { res = res / n * (n - 1); } return res; } static int pow(int a, int b, int mod) { int res = 1; while (b > 0) { if ((b & 1) != 0) { res = res * a % mod; } a = a * a % mod; b >>= 1; } return res; } static boolean check(int a, int n, int phi) { for (int i = 1, j = pow(a, i, n); i <= phi; i++, j = j * a % n) { if (j == 1) { return false; } } return true; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int n = scanner.nextInt(); int phi = phi(n); List<Integer> ans = new ArrayList<>(); for (int i = 1; i < n; i++) { if (gcd(i, n) == 1 && check(i, n, phi)) { ans.add(i); } } Collections.sort(ans); for (int x : ans) { System.out.print(x + " "); } System.out.println(); } } } ``` 其中,gcd 函数用于求最大公约数,phi 函数用于求欧拉函数,pow 函数用于快速幂求模,check 函数用于判断一个数是否是原根。在主函数中,我们依次读入每个 n,求出 phi(n),然后枚举模 n 意义下的所有数,判断它是否是原根,将所有原根存入一个 List 中,最后排序输出即可。 希望我的回答能够帮到你,如果你有任何问题,欢迎随时提出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值