题目:
给定一个
n
n
n和两个向量
v
1
⃗
=
(
d
x
1
,
d
y
1
)
,
v
2
⃗
=
(
d
x
2
,
d
y
2
)
\vec{v_1}=(dx_1,dy_1),\vec{v_2}=(dx_2,dy_2)
v1=(dx1,dy1),v2=(dx2,dy2),在平面上找出
n
n
n个不相同的点
(
x
1
,
y
1
)
,
(
x
2
,
y
2
)
,
.
.
.
,
(
x
n
,
y
n
)
(x_1,y_1),(x_2,y_2),...,(x_n,y_n)
(x1,y1),(x2,y2),...,(xn,yn),使平面上任意一个点
(
x
,
y
)
(x,y)
(x,y)恰好有一个
(
x
i
,
y
i
)
(x_i,y_i)
(xi,yi)满足
(
x
,
y
)
=
(
x
i
,
y
i
)
+
v
1
⃗
+
v
2
⃗
(x,y)=(x_i,y_i)+\vec{v_1}+\vec{v_2}
(x,y)=(xi,yi)+v1+v2。能找到输出YES,并输出找到的点,否则输出NO。
(
1
≤
n
≤
1
0
5
,
−
1
0
6
≤
d
x
1
,
d
y
1
,
d
x
2
,
d
y
2
≤
1
0
6
)
(1 \le n \le 10^5,-10^6 \le dx_1,dy_1,dx_2,dy_2 \le 10^6)
(1≤n≤105,−106≤dx1,dy1,dx2,dy2≤106)
题解:
这道题是有纯数学做法的,但是我不会。这里介绍一种几何的做法。
画出从原点出发可以到达的所有的点
就是上图平行四边形网格的顶点,那么
(
0
,
0
)
(0,0)
(0,0)到不了的点就是平行四边形内部和边上的整点。显然每个平行四边形内部和边上的整点都可以由相邻平行四边形相同位置的点到达,且这些点相互不可达,所以只要将阴影部分的平行四边形内部和边上的整点作为要找出的点(包括左边和下边,不包括上边和右边,因为上边和右边的点可以由左边和下边的点到达)就行了,如果这些点的个数等于
n
n
n个,则为YES,不然为NO。
细节很多,还卡精度。
复杂度: O ( n ) O(n) O(n)
代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<bitset>
#include<sstream>
#include<ctime>
//#include<chrono>
//#include<random>
//#include<unordered_map>
using namespace std;
#define ll long long
#define ls o<<1
#define rs o<<1|1
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz(x) (int)(x).size()
#define all(x) (x).begin(),(x).end()
const double pi=acos(-1.0);
const double eps=1e-9;
const int mod=1e9+7;
const int INF=0x3f3f3f3f;
const int maxn=1e5+5;
ll read(){
ll x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
//符号函数
int sgn(double x){
if(fabs(x)<eps)return 0;
else if(x>0)return 1;
else return -1;
}
//浮点数比较
int dcmp(double x,double y){
if(fabs(x-y)<eps)return 0;
else if(x>y)return 1;
else return -1;
}
//点
struct Point{
double x,y;
Point(double x=0,double y=0):x(x),y(y){}
// int Qudrant()const{//极角排序,原点
// if(x>0&&y>=0)return 1;
// if(x<=0&&y>0)return 2;
// if(x<0&&y<=0)return 3;
// if(x>=0&&y<0)return 4;
// }
// bool operator<(const Point &ot)const{
// if(Qudrant()==ot.Qudrant()){
// return 1ll*x*ot.y-1ll*y*ot.x>0;
// }
// return Qudrant()<ot.Qudrant();
// }
};
//向量
typedef Point Vector;
//向量加法
Vector operator+(Vector a,Vector b){
return Vector(a.x+b.x,a.y+b.y);
}
//向量减法
Vector operator-(Vector a,Vector b){
return Vector(a.x-b.x,a.y-b.y);
}
//向量数乘
Vector operator*(Vector a,double p){
return Vector(a.x*p,a.y*p);
}
Vector operator/(Vector a,double p){
return Vector(a.x/p,a.y/p);
}
//两个点判等
bool operator==(const Point &a,const Point &b){
if(dcmp(a.x,b.x)==0&&dcmp(a.y,b.y)==0)return true;
else return false;
}
//点积
double Dot(Vector a,Vector b){
return a.x*b.x+a.y*b.y;
}
//叉积
double Cross(Vector a,Vector b){
return a.x*b.y-a.y*b.x;
}
//模长
double Length(Vector a){
return sqrt(Dot(a,a));
}
//判断直线和线段相交
bool SegmentLineIntersection(Point P,Vector v,Point a,Point b){
double c1=Cross(v,a-P),c2=Cross(v,b-P);
if(!sgn(c1)||!sgn(c2)){
if(!sgn(c1)&&!sgn(c2)){
return false;
}
else return true;
}
else{
return sgn(c1)*sgn(c2)<0;
}
}
//计算两直线交点(P+tv,Q+tw),在调用前要保证两直线相交
Point GetLineIntersection(Point P,Vector v,Point Q,Vector w){
Vector u=P-Q;
double t=Cross(w,u)/Cross(v,w);
return P+v*t;
}
int n,dx1,dy1,dx2,dy2;
vector<pii>ans;
int main(void){
// freopen("in.txt","r",stdin);
scanf("%d",&n);
scanf("%d%d%d%d",&dx1,&dy1,&dx2,&dy2);
if(dx1<0){
dx1*=-1;
dy1*=-1;
}
if(dx2<0){
dx2*=-1;
dy2*=-1;
}
if((dx1==0&&dy1==0)||(dx2==0&&dy2==0)||(dx1==dx2&&dy1==dy2)){
puts("NO");
return 0;
}
Point p1=Point(dx1,dy1),p2=Point(dx2,dy2),p3=Point(0,0),p4=Point(dx1+dx2,dy1+dy2),p;
Vector v=Vector(0,1),v1=Vector(dx1,dy1),v2=Vector(dx2,dy2);
int f=1;
for(int i=0;i<=dx1+dx2-1;i++){
if(i==dx1+dx2)continue;
p=Point(i,0);
Point down,up;
int fd,fu;
if(SegmentLineIntersection(p,v,p1,p4)){
down=GetLineIntersection(p,v,p1,v2);
fd=0;
}
else{
down=GetLineIntersection(p,v,p3,v1);
fd=1;
}
if(SegmentLineIntersection(p,v,p2,p4)){
up=GetLineIntersection(p,v,p2,v1);
fu=0;
}
else{
up=GetLineIntersection(p,v,p3,v2);
fu=1;
}
if(dcmp(down.y,up.y)>0){
swap(down,up);
swap(fd,fu);
}
int d,u;
if(fd==1){
if(sgn(down.y-round(down.y))==0){
d=round(down.y);
}
else{
if(sgn(down.y)<0){
d=(int)down.y;
}
else{
d=(int)down.y+1;
}
}
}
else{
if(sgn(down.y-round(down.y))==0){
d=round(down.y)+1;
}
else{
if(sgn(down.y)<0){
d=(int)down.y;
}
else{
d=(int)down.y+1;
}
}
}
if(fu==1){
if(sgn(up.y-round(up.y))==0){
u=round(up.y);
}
else{
if(sgn(up.y)<0){
u=(int)up.y-1;
}
else{
u=(int)up.y;
}
}
}
else{
if(sgn(up.y-round(up.y))==0){
u=round(up.y)-1;
}
else{
if(sgn(up.y)<0){
u=(int)up.y-1;
}
else{
u=(int)up.y;
}
}
}
for(int j=d;j<=u;j++){
ans.pb(mp(i,j));
if(sz(ans)>n){
f=0;
break;
}
}
if(!f)break;
}
if(sz(ans)!=n){
puts("NO");
}
else{
puts("YES");
for(auto v:ans){
printf("%d %d\n",v.fi,v.se);
}
}
return 0;
}