集训队专题(8)1007 Remainder

Remainder

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3625    Accepted Submission(s): 846


Problem Description
Coco is a clever boy, who is good at mathematics. However, he is puzzled by a difficult mathematics problem. The problem is: Given three integers N, K and M, N may adds (‘+’) M, subtract (‘-‘) M, multiples (‘*’) M or modulus (‘%’) M (The definition of ‘%’ is given below), and the result will be restored in N. Continue the process above, can you make a situation that “[(the initial value of N) + 1] % K” is equal to “(the current value of N) % K”? If you can, find the minimum steps and what you should do in each step. Please help poor Coco to solve this problem. 

You should know that if a = b * q + r (q > 0 and 0 <= r < q), then we have a % q = r.
 

Input
There are multiple cases. Each case contains three integers N, K and M (-1000 <= N <= 1000, 1 < K <= 1000, 0 < M <= 1000) in a single line.

The input is terminated with three 0s. This test case is not to be processed.
 

Output
For each case, if there is no solution, just print 0. Otherwise, on the first line of the output print the minimum number of steps to make “[(the initial value of N) + 1] % K” is equal to “(the final value of N) % K”. The second line print the operations to do in each step, which consist of ‘+’, ‘-‘, ‘*’ and ‘%’. If there are more than one solution, print the minimum one. (Here we define ‘+’ < ‘-‘ < ‘*’ < ‘%’. And if A = a1a2...ak and B = b1b2...bk are both solutions, we say A < B, if and only if there exists a P such that for i = 1, ..., P-1, ai = bi, and for i = P, ai < bi)
 

Sample Input
  
2 2 2 -1 12 10 0 0 0
 

Sample Output
  
0 2 *+
 

Author
Wang Yijie
 

这题的思路也很简单,只要通过四钟运算方法得到答案,唯一需要注意的就是由于减法可能会造成取余的结果为负数,所以我们还是在取余的时候采取((a%n)+n)%n的形式避免这种情况,其他关于对余数的把控也就是对结果是否为0的情况判定和我在上一题1006 Yet Another Multiple Problem中的讲解时一样的。

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = 1e6+10;
char action[5] = "+-*%";
int vis[maxn],c[maxn];
int mod,n,k,m;
struct node
{
	int from,w;
}edge[maxn];
void print(int p,int step)
{
	if(edge[p].from == -1)
	{
		printf("%d\n",step);
		return ;
	}
	print(edge[p].from,step+1);
	printf("%c",action[edge[p].w]);
}
void bfs()
{
	memset(vis,0,sizeof(vis));
	int i,j,p,ans,b,d,u,v;
	for(i=0; i<mod; i++) edge[i].from = -1;
	queue<int> q;
	ans = ((n+1)%k+k)%k;
	p = (n%mod + mod)%mod;
	vis[p] = 1;
	q.push(p);
	while(!q.empty())
	{
		p = q.front();
		q.pop();
		if(p%k == ans)
		{
			print(p,0);
			printf("\n");
			return ;
		}
		for(i=0; i<4; i++)
		{
			if(i == 0) v = (p+m)%mod;
			else if(i == 1) v = ((p-m)%mod+mod)%mod;
			else if(i == 2) v = (p*m)%mod;
			else v = p%m;
			if(vis[v]) continue;
			vis[v] = 1;
			edge[v].from = p;
			edge[v].w = i;
			q.push(v);
		}
	}
	printf("0\n");
}
int main()
{
	while(scanf("%d%d%d",&n,&k,&m),n||m||k)
	{
		mod = m*k;
		bfs();
	}
	return 0;
}


### BigDecimal 的 `remainder` 方法详解 Java 中的 `BigDecimal` 类提供了多种用于执行精确算术操作的方法,其中就包括计算两个数值相除后的余数。对于此类需求,可以使用 `remainder(BigDecimal divisor)` 或者带有舍入模式参数的重载版本 `remainder(BigDecimal divisor, MathContext mc)`。 #### 使用无参版 `remainder` 当调用不带额外参数的 `remainder()` 函数时,该函数会返回被除数减去商乘以除数的结果作为余数[^3]: ```java import java.math.BigDecimal; public class RemainderExample { public static void main(String[] args) { BigDecimal dividend = new BigDecimal("10"); BigDecimal divisor = new BigDecimal("3"); // 计算并打印余数 System.out.println(dividend.remainder(divisor)); } } ``` 这段代码展示了如何创建两个 `BigDecimal` 对象,并通过其中一个对象上调用 `remainder()` 来获取另一个对象做为除数情况下的余数。 #### 带有 `MathContext` 参数的 `remainder` 如果希望指定四舍五入的方式以及精度,则可以选择第二个形式——传入一个 `MathContext` 实例给 `remainder()` 方法。这允许更精细地控制最终结果的小数位数和处理方式[^4]: ```java import java.math.BigDecimal; import java.math.MathContext; import java.math.RoundingMode; public class RoundedRemainderExample { public static void main(String[] args) { BigDecimal num1 = new BigDecimal("987654321.123456789"); BigDecimal num2 = new BigDecimal("123456789.987654321"); // 创建具有特定设置的 MathContext MathContext mc = new MathContext(4, RoundingMode.HALF_UP); // 执行带有上下文配置的取模运算 System.out.println(num1.remainder(num2, mc)); } } ``` 在这个例子中,定义了一个拥有四位有效数字并且采用向上取整策略(`HALF_UP`)的 `MathContext` 对象来影响 `remainder()` 调用的行为。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值