跳到正文

题解

题解:UVA13262 Count Equation Solutions

Comet387约 6 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 6 分钟
获赞
2
题目
UVA13262

版本

  1. v1当前

题面翻译

给定一个关于六个正整数变量 x1,x2,x3,x4,x5,x6x_1, x_2, x_3, x_4, x_5, x_6 的方程:

a1x1−a2x2+a3x3−a4x4+a5x5−a6x6=0a_1 x_1 - a_2 x_2 + a_3 x_3 - a_4 x_4 + a_5 x_5 - a_6 x_6 = 0

其中,a1,a2,a3,a4,a5,a6a_1, a_2, a_3, a_4, a_5, a_6 为整数,变量 xix_i 满足 1≤xi≤M1 \leq x_i \leq M。计算方程在所有可能的 x1,x2,x3,x4,x5,x6x_1, x_2, x_3, x_4, x_5, x_6 取值情况下的解的数量。

题目思路

可以将原方程重写为:

a1x1+a3x3+a5x5=a2x2+a4x4+a6x6a_1 x_1 + a_3 x_3 + a_5 x_5 = a_2 x_2 + a_4 x_4 + a_6 x_6

这样就可以分解为两部分:

  • 左侧的和:a1x1+a3x3+a5x5a_1 x_1 + a_3 x_3 + a_5 x_5;
  • 右侧的和:a2x2+a4x4+a6x6a_2 x_2 + a_4 x_4 + a_6 x_6。

我们的任务就是分别计算出所有可能的左侧和与右侧和,然后统计它们相等的情况,这个可以用 Meet in the middle 的思想。

解法说明

  1. 由于 x1,x3,x5x_1, x_3, x_5 都在 [1,M][1, M] 的范围内,直接 O(M3)\mathcal O(M^3) 枚举出所有可能的组合,计算出所有可能的左侧和 ll;
  2. 同样地,枚举出 x2,x4,x6x_2, x_4, x_6 的所有可能值,计算出所有可能的右侧和 rr;
  3. 最后找 ll 中的和与 rr 中的和相等的情况,可以用双指针解决。

时间复杂度

对于每组数据:

  • 枚举左侧和右侧的所有和可能的组合是 O(M3)\mathcal O(M^3);
  • sort 的时间复杂度是 O(M3log⁡M3)\mathcal O(M^3 \log M^3),但是会 TLE,所以用基数排序,近似 O(M3)\mathcal O(M^3)。
  • 双指针是 O(M3)\mathcal O(M^3)。

因此,TT 组数据是 O(TM3)\mathcal O(T M^3)。

Code

C++
#include<bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(int i(a);i<=(b);++i)
#define req(i,a,b) for(int i(a);i>=(b);--i)
#define sort stable_sort
using namespace std;
char buf[1<<23],*p1=buf,*p2=buf,ubuf[1<<23],*u=ubuf;
template<typename TP> inline TP read(TP &num)
{
	TP x=0;
	int f=0;
	char ch=getchar();
	while(ch<48||ch>57) f|=ch=='-',ch=getchar();
	while(48<=ch&&ch<=57) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
	return num=f?-x:x;
}
template<typename ...Args> inline void read(Args &...args)
{
	(read(args),...);
}
template<typename TP> inline void write(TP x)
{
	(x<0)?(putchar('-'),x=-x):0;
	(x>9)?(write(x/10),0):0;
	putchar((x%10)^48);
}
template<typename TP> inline void writeln(TP x)
{
	write<TP>(x);
	puts("");
}
void rdx_sort(int *a,int n)
{
	const int B=256;
	int *o=new int[n],c[B]={0},*in=a,*tmp=o;
	rep(p,0,3)
	{
		fill(c,c+B,0);
		rep(i,0,n-1) ++c[(in[i]>>(p*8))&0xFF];
		int t=0,tt=0;
		rep(i,0,B-1) tt=c[i],c[i]=t,t+=tt;
		rep(i,0,n-1)
		{
			int tt=(in[i]>>(p*8))&0xFF;
			tmp[c[tt]++]=in[i];
		}
		swap(in,tmp);
	}
	if(in!=a) memcpy(a,o,sizeof(int)*n);
	delete []o;
}
int m,l[1000001],r[1000001],a[7];
signed main()
{
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	while(cin>>m)
	{
		rep(i,0,5) read(a[i]);
		int ax1[101],ax3[101],ax5[101],ax2[101],ax4[101],ax6[101];
		rep(i,1,m) ax1[i]=a[0]*i,ax3[i]=a[2]*i,ax5[i]=a[4]*i,ax2[i]=a[1]*i,ax4[i]=a[3]*i,ax6[i]=a[5]*i;
		int cntl=0,cntr=0;
		rep(x1,1,m)
		{
			int sum1=ax1[x1];
			rep(x3,1,m)
			{
				int sum2=sum1+ax3[x3];
				rep(x5,1,m) l[cntl++]=sum2+ax5[x5];
			}
		}
		int sizl=cntl;
		rep(x2,1,m)
		{
			int sum1=ax2[x2];
			rep(x4,1,m)
			{
				int sum2=sum1+ax4[x4];
				rep(x6,1,m) r[cntr++]=sum2+ax6[x6];
			}
		}
		int sizr=cntr;
		rdx_sort(l,sizl);
		rdx_sort(r,sizr);
		int i=0,j=0,cnt=0;
		while(i<sizl&&j<sizr)
		{
			if(l[i]==r[j])
			{
				int cur=l[i],cl=0,cr=0;
				while(i<sizl&&l[i]==cur) ++cl,++i;
				while(j<sizr&&r[j]==cur) ++cr,++j;
				cnt+=cl*cr;
			}
			else if(l[i]<r[j]) ++i;
			else ++j;
		}
		writeln(cnt);
	}
	return 0;
}

只跑了 10.5s,常数优秀。

原文