The 13th Blue Bridge Cup Provincial E: X-ary subtraction (greedy)

Output a line of one integer, representing the smallest possible value of the result of the X-base number AB converted to decimal and then modulo 1e9+7.

Analysis, this is actually a greedy problem. Let's first look at what determines the weight of each number. Let's take the number given in the question. The bit where 321 and 3 are located is octal. , the digit where 2 is located is decimal, and the digit where 1 is located is binary. Obviously, it can be known that every 2 lowest digits can be entered into a second digit, and every 10 second digits can be entered into a first digit, that is Every 2*10 third bits can be entered into a first bit. We may find that the weight of the i-th bit is actually the product of the bases of the bits lower than the i-th bit , analogous to what we are used to. Binary and decimal are also easy to understand, I won't say more about this

Since we already know that A is larger than B, in order to make AB as small as possible, we should make the high-level weights as small as possible , and we can ignore the influence of low-level weights. Why? Because if the weight of the high position decreases by 1, it will have a greater impact than the sum of all the low positions, so our greedy strategy is to reduce the weight of the high position. Combined with our analysis of the factors affecting the weight, we can know that when the low position is on the When the base of each digit is taken to the minimum, the weight of the high position is the smallest at this time, because each digit gives two numbers a and b, because the base cannot be less than or equal to a or less than or equal to b, so We only need to make the base of the bit be the maximum value of a and b plus 1 .

Here is the code:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<map>
#include<set>
using namespace std;
const int N=1e6+10;
const int mod=1e9+7;
long long a[N],b[N];
int main()
{
	int n,lena,lenb;
	scanf("%d%d",&n,&lena);
	for(int i=lena;i>=1;i--)
		scanf("%lld",&a[i]);
	scanf("%d",&lenb);
	for(int i=lenb;i>=1;i--)
		scanf("%lld",&b[i]);
	long long ans=0,mul=1;
	for(int i=1;i<=lena;i++)
	{
		ans=(ans+(a[i]-b[i])*mul)%mod;
		long long t=max(a[i],b[i])+1;
		if(t<2) t=2;
		mul=mul*t%mod;
	}
	printf("%lld",ans);
	return 0;
}

Guess you like

Origin blog.csdn.net/AC__dream/article/details/124066398