/*
ID:kevin_s1
PROG:ariprog
LANG:C++
*/ #include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <list>
#include <cmath> using namespace std; //gobal variable====
int doubleSqure[999999];
int table[999999];
int _index;
int M,N;
int lim; struct node{
int a;
int d; }; vector<node> result;
//================== //function==========
void init(){
memset(table,0,sizeof(table));
_index = 1;
for(int i = 0; i <= M; i++){
for(int j = 0; j <= M; j++){
int num = i * i + j * j;
if(table[num] == 0){
doubleSqure[_index++] = num;
table[num] = 1;
}
}
}
_index--;
} void deal(int x){
int num = doubleSqure[x];
for(int i = x + 1; i <= _index - N + 2; i++){
if(doubleSqure[x] + (doubleSqure[i] - doubleSqure[x])*(N - 1) > lim)
break;
<span style="white-space:pre">		</span>//剪枝。没有这一步的话会第七组数据会超时
int d = doubleSqure[i] - num;
bool flag = true;
int count = doubleSqure[i];
for(int j = 3; j <= N; j++){
count += d;
if(table[count] == 0)
flag = false;
}
if(flag){
node no;
no.a = num;
no.d = d;
result.push_back(no);
}
}
} bool cmp(const node& n1, const node& n2){
if(n1.d == n2.d)
return n1.a < n2.a;
else
return n1.d < n2.d;
} //================== int main(){
freopen("ariprog.in","r",stdin);
freopen("ariprog.out","w",stdout);
cin>>N>>M;
init();
sort(doubleSqure, doubleSqure + _index);
lim = doubleSqure[_index]; for(int i = 1; i <= _index - N + 1; i++){
deal(i);
}
sort(result.begin(), result.end(), cmp);
vector<node>::iterator iter;
if(result.size() == 0)
cout<<"NONE"<<endl;
else{
for(iter = result.begin(); iter != result.end(); iter++){
cout<<iter->a<<" "<<iter->d<<endl;
}
}
return 0;
}

USACO ariprog 暴力枚举+剪枝的更多相关文章

  1. 【poj 3080】Blue Jeans(字符串--KMP+暴力枚举+剪枝)

    题意:求n个串的字典序最小的最长公共子串. 解法:枚举第一个串的子串,与剩下的n-1个串KMP匹配,判断是否有这样的公共子串.从大长度开始枚举,找到了就break挺快的.而且KMP的作用就是匹配子串, ...

  2. POJ 2531 Network Saboteur (枚举+剪枝)

    题意:给你一个图,图中点之间会有边权,现在问题是把图分成两部分,使得两部分之间边权之和最大. 目前我所知道的有四种做法: 方法一:状态压缩 #include <iostream> #inc ...

  3. [ACM] ZOJ 3816 Generalized Palindromic Number (DFS,暴力枚举)

    Generalized Palindromic Number Time Limit: 2 Seconds      Memory Limit: 65536 KB A number that will ...

  4. 【暴力枚举&BFS】Flow Free @RMRC2017/upcexam5124

    时间限制: 1 Sec 内存限制: 128 MB 题目描述 Flow Free is a puzzle that is played on a 2D grid of cells, with some ...

  5. [POJ3612] Telephone Wire(暴力dp+剪枝)

    [POJ3612] Telephone Wire(暴力dp+剪枝) 题面 有N根电线杆,初始高度为h[i],要给相邻的两根连线.可以选择拔高其中一部分电线杆,把一根电线杆拔高\(\Delta H\)的 ...

  6. CodeForces 742B Arpa’s obvious problem and Mehrdad’s terrible solution (暴力枚举)

    题意:求定 n 个数,求有多少对数满足,ai^bi = x. 析:暴力枚举就行,n的复杂度. 代码如下: #pragma comment(linker, "/STACK:1024000000 ...

  7. 2014牡丹江网络赛ZOJPretty Poem(暴力枚举)

    /* 将给定的一个字符串分解成ABABA 或者 ABABCAB的形式! 思路:暴力枚举A, B, C串! */ 1 #include<iostream> #include<cstri ...

  8. HNU 12886 Cracking the Safe(暴力枚举)

    题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12886&courseid=274 解题报告:输入4个数 ...

  9. 51nod 1116 K进制下的大数 (暴力枚举)

    题目链接 题意:中文题. 题解:暴力枚举. #include <iostream> #include <cstring> using namespace std; ; ; ch ...

随机推荐

  1. ASP.NET Zero--1.如何开始

    1.加群 群号:104390185,下载这个文件并解压 用VS2015打开aspnet-zero-1.9.0.1 2.修改配置文件 Web项目web.config连接字符串 <add name= ...

  2. eclipse重的自动提示与行号和快捷图标的显示

    显示行号:Window->Preferences->Gerenal->Editors->Text Editors然后在show line number上打对勾自动提示:Wind ...

  3. anaconda-anaconda无法安装pydot?

    只需先安装graphviz,再安装pydot就可以了 sudo conda install graphviz sudo conda install pydot

  4. Mac 命令行美化

    在 mac 中使用原生的命令行工具,竟然没有 git 命令的自动补全,在 git 仓库下也看不到当前的分支名,不能忍.于是,开始一波改造. 目标:命名 Tab 自动补全:可以显示分支名: 一番 Goo ...

  5. Python开发基础-Day25-28FTP项目(待补充)

    optparse C:\Users\Mr.chai>python C:/Users/Mr.chai/Desktop/PythonProject/DAY/day27/LuffyFTP/client ...

  6. BZOJ 3676: [Apio2014]回文串 后缀自动机 Manacher 倍增

    http://www.lydsy.com/JudgeOnline/problem.php?id=3676 过程很艰难了,第一次提交Manacher忘了更新p数组,超时,第二次是倍增的第0维直接在自动机 ...

  7. hdu 3642 体积并

    题意:求三个矩形体积的并 链接:点我 枚举z #include<stdio.h> #include<iostream> #include<stdlib.h> #in ...

  8. Makefile-函数patsubst

    比方说你在makefile里定义了一个变量,内容是一堆 .c 文件的的名字,如 SRC = aaa.c bbb.c my.c his.c你可以用 patsubst 根据某种模式,将这些名字改成另外的, ...

  9. Problem G: 深入浅出学算法008-求佩尔方程的解

    Description 求关于x y的二次不定方程的解 x2-ny2=1 Input 多组输入数据,先输入组数T 然后输入正整数n(n<=100) Output 对于每组数据输出一行,求y< ...

  10. [转][Android]Android数据的四种存储方式

    android.database.sqlite类 SQLiteQueryBuilder java.lang.Object android.database.sqlite.SQLiteQueryBuil ...