题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4631
数据是随机的,没有极端数据,所以可以分段考虑,最小值是一个单调不增的函数,然后每次分治算平面最近点对就可以了。。。
//STATUS:G++_AC_10390MS_23804KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End struct Node{
LL x,y;
LL id,index;
Node(){}
Node(LL _x,LL _y,LL _index):x(_x),y(_y),index(_index){}
}p[N],nod[N],temp[N]; int n; LL dist(Node &a,Node &b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
} int cmpxy(Node a,Node b)
{
return a.x!=b.x?a.x<b.x:a.y<b.y;
} int cmpy(Node a,Node b)
{
return a.y<b.y;
} pii Closest_Pair(int l,int r)
{
if(l==r || l+==r)return pii(l,r);
LL d,d1,d2;
int i,j,k,mid=(l+r)/;
pii pn1=Closest_Pair(l,mid);
pii pn2=Closest_Pair(mid+,r);
d1=(pn1.first==pn1.second?LNF:dist(nod[pn1.first],nod[pn1.second]));
d2=(pn2.first==pn2.second?LNF:dist(nod[pn2.first],nod[pn2.second]));
pii ret;
d=Min(d1,d2);
ret=d1<d2?pn1:pn2;
for(i=l,k=;i<=r;i++){
if((nod[mid].x-nod[i].x)*(nod[mid].x-nod[i].x)<=d){
temp[k++]=nod[i];
}
}
sort(temp,temp+k,cmpy);
for(i=;i<k;i++){
for(j=i+;j<k && (temp[j].y-temp[i].y)*(temp[j].y-temp[i].y)<d;j++){
if(dist(temp[i],temp[j])<d){
d=dist(temp[i],temp[j]);
ret=make_pair(temp[i].id,temp[j].id);
}
}
} return ret;
} void Init()
{
int i;
LL x,y,Ax,Bx,Cx,Ay,By,Cy;
cin>>n>>Ax>>Bx>>Cx>>Ay>>By>>Cy;
x=y=;
for(i=;i<n;i++){
x=(x*Ax+Bx)%Cx;
y=(y*Ay+By)%Cy;
p[i]=Node(x,y,i);
}
} int main(){
// freopen("in.txt","r",stdin);
int T,i,j,k;
LL ans,hig;
scanf("%d",&T);
while(T--)
{
Init(); int end=n;
pii t;
ans=;
while(end>){
for(i=;i<end;i++)nod[i]=p[i];
sort(nod,nod+end,cmpxy);
for(i=;i<end;i++)nod[i].id=i;
t=Closest_Pair(,end-);
hig=Max(nod[t.first].index,nod[t.second].index);
ans+=(end-hig)*dist(nod[t.first],nod[t.second]);
end=hig;
}
cout<<ans<<endl;
}
return ;
}