[AGC025D]Choosing Points

题目大意:

输⼊\(n(n\le300),d_1,d_2\),你要找到\(n^2\)个整点\((x,y)\)满⾜\(0\le x,y<2n\)。并且找到的任意两个点距离,既不是\(\sqrt{d_1}\),也不是\(\sqrt{d_2}\)。

思路:

所有距离为\(\sqrt{d_1}\)的点连边,可以得到一个⼆分图。\(d_2\)同理。注意到满足\(a^2+b^2=d\)的\((a,b)\)只有\(\mathcal O(n)\)个,可以得到\(\mathcal O(n^3)\)的二分图染色做法。

源代码:

#include<cmath>
#include<cstdio>
#include<cctype>
#include<vector>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
const int N=600;
int n,d1,d2,col[2][N*N];
std::vector<std::pair<int,int>> v[2];
inline int id(const int &x,const int &y) {
return x*N+y;
}
inline bool check(const int &x) {
return 0<=x&&x<n*2;
}
void dfs(const int &x,const int &t) {
const int i=x/N,j=x%N;
for(auto &d:v[t]) {
const int nx=i+d.first,ny=j+d.second;
if(!check(nx)||!check(ny)) continue;
const int y=id(i+d.first,j+d.second);
if(!col[t][y]) {
col[t][y]=col[t][x]==1?2:1;
dfs(y,t);
}
}
}
int main() {
n=getint(),d1=getint(),d2=getint();
for(register int i=0;i<=d1;i++) {
const int j=sqrt(d1-i*i);
if(i*i+j*j!=d1) continue;
v[0].emplace_back(std::make_pair(i,j));
v[0].emplace_back(std::make_pair(-i,j));
v[0].emplace_back(std::make_pair(i,-j));
v[0].emplace_back(std::make_pair(-i,-j));
}
for(register int i=0;i<=d2;i++) {
const int j=sqrt(d2-i*i);
if(i*i+j*j!=d2) continue;
v[1].emplace_back(std::make_pair(i,j));
v[1].emplace_back(std::make_pair(-i,j));
v[1].emplace_back(std::make_pair(i,-j));
v[1].emplace_back(std::make_pair(-i,-j));
}
for(register int i=0;i<n*2;i++) {
for(register int j=0;j<n*2;j++) {
if(!col[0][id(i,j)]) {
col[0][id(i,j)]=1;
dfs(id(i,j),0);
}
if(!col[1][id(i,j)]) {
col[1][id(i,j)]=1;
dfs(id(i,j),1);
}
}
}
for(register int i=0,cnt=0;i<n*2;i++) {
for(register int j=0;j<n*2;j++) {
if(col[0][id(i,j)]==1&&col[1][id(i,j)]==1) {
printf("%d %d\n",i,j);
if(++cnt==n*n) return 0;
}
}
}
}

[AGC025D]Choosing Points的更多相关文章

  1. 「AGC025D」 Choosing Points

    「AGC025D」 Choosing Points 神仙构造题. 首先你会尝试暴力做,先随便选一个点,然后把当前能选得全选上,然后你发现这样样例都过不了. 然后我们可以这样考虑:你把距离为 \(\sq ...

  2. 2018.07.12 atcoder Choosing Points(数学分析好题)

    传送门 一句话题意:给出n,d1,d2" role="presentation" style="position: relative;">n,d ...

  3. AtCoder Grand Contest 025 Problem D - Choosing Points

    题目大意:输入$n,d1,d2$,你要找到$n^2$个整点 x, y 满足$0 \leqslant x, y<2n$.并且找到的任意两个点距离,既不是$\sqrt{d1}$,也不是 $\sqrt ...

  4. atcoder题目合集(持续更新中)

    Choosing Points 数学 Integers on a Tree 构造 Leftmost Ball 计数dp+组合数学 Painting Graphs with AtCoDeer tarja ...

  5. 【AtCoder】AGC025题解

    A - Digits Sum 枚举即可 代码 #include <bits/stdc++.h> #define fi first #define se second #define pii ...

  6. AGC025简要题解

    AGC025简要题解 B RGB Coloring 一道简单题,枚举即可. C Interval Game 考虑可以进行的操作只有两种,即左拉和右拉,连续进行两次相同的操作是没有用的. 左拉时肯定会选 ...

  7. 有理数的稠密性(The rational points are dense on the number axis.)

    每一个实数都能用有理数去逼近到任意精确的程度,这就是有理数的稠密性.The rational points are dense on the number axis.

  8. [LeetCode] Max Points on a Line 共线点个数

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  9. CF219D. Choosing Capital for Treeland [树形DP]

    D. Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes i ...

随机推荐

  1. python作业三级菜单day1(第一周)

    一.作业需求: 1. 运行程序输出第一级菜单 2. 选择一级菜单某项,输出二级菜单,同理输出三级菜单 3. 菜单数据保存在文件中 4. 让用户选择是否要退出 5. 有返回上一级菜单的功能 二三级菜单文 ...

  2. python碎片记录(二)

    1.字典中嵌套字典使用 dict={'a':{1:2,2:3}} print(dict) print(dict['a'][2]) 输出如下: {'a': {1: 2, 2: 3}} 3  2.元组与l ...

  3. CentOS7修改默认运行级别

    新装了一个虚拟机,图形界面启动太慢,想调整一下按照以前的经验改运行级别,输入: vi /etc/inittab 然后发现跟之前不一样了,在inittab设置不再生效: # inittab is no ...

  4. Android控件——监听按钮的点击事件

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAroAAAFTCAIAAABZPDiZAAAgAElEQVR4nOy9918UWfb///1jdu2uBs

  5. Go语言大神亲述:历七劫方可成为程序员!

    “历劫1”:你坚信你可以用Go来做面向对象编程? 在经历了一次Go应用之旅之后,你可能就会开始思考:“怎么样才能让这种语言更像面向对象的编程语言?”因为你已经习惯了这种编程,你想要制作健壮的代码.想要 ...

  6. IE11中实现颜色渐变

    background: -ms-linear-gradient(left,#daa23e,#ad7f27); 下面是css3中颜色渐变对各个浏览器的写法:background: -webkit-lin ...

  7. python初学-列表

    列表操作: 列表一般需要先调用方法后才能打印,不能直接打印调用的方法 因为列表可以修改 一般不会返回一个新列表 # 列表 # new_names = ['lzc','lzc2','lzc3'] # 下 ...

  8. 看懂sh脚本

    1 开头 程序必须以下面的行开始(必须方在文件的第一行): #!/bin/sh 符号#!用来告诉系统它后面的参数是用来执行该文件的程序.在这个例子中我们使用/bin/sh来执行程序. 当编写脚本完成时 ...

  9. experss 做小型服务器出现跨域问题

    情况是这样的 我用express做一个小型的服务器来做我demo项目的一个接口 然后我就出现了跨域问题 然后我就 app.all('/*', function(req, res, next) { // ...

  10. Search Insert Position——二分法

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...