题解:UVA13262 Count Equation Solutions

· · 题解

题面翻译

给定一个关于六个正整数变量 x_1, x_2, x_3, x_4, x_5, x_6 的方程:

a_1 x_1 - a_2 x_2 + a_3 x_3 - a_4 x_4 + a_5 x_5 - a_6 x_6 = 0

其中,a_1, a_2, a_3, a_4, a_5, a_6 为整数,变量 x_i 满足 1 \leq x_i \leq M。计算方程在所有可能的 x_1, x_2, x_3, x_4, x_5, x_6 取值情况下的解的数量。

题目思路

可以将原方程重写为:

a_1 x_1 + a_3 x_3 + a_5 x_5 = a_2 x_2 + a_4 x_4 + a_6 x_6

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

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

解法说明

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

时间复杂度

对于每组数据:

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

Code

#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,常数优秀。