USACO ariprog 暴力枚举+剪枝
/*
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 暴力枚举+剪枝的更多相关文章
- 【poj 3080】Blue Jeans(字符串--KMP+暴力枚举+剪枝)
题意:求n个串的字典序最小的最长公共子串. 解法:枚举第一个串的子串,与剩下的n-1个串KMP匹配,判断是否有这样的公共子串.从大长度开始枚举,找到了就break挺快的.而且KMP的作用就是匹配子串, ...
- POJ 2531 Network Saboteur (枚举+剪枝)
题意:给你一个图,图中点之间会有边权,现在问题是把图分成两部分,使得两部分之间边权之和最大. 目前我所知道的有四种做法: 方法一:状态压缩 #include <iostream> #inc ...
- [ACM] ZOJ 3816 Generalized Palindromic Number (DFS,暴力枚举)
Generalized Palindromic Number Time Limit: 2 Seconds Memory Limit: 65536 KB A number that will ...
- 【暴力枚举&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 ...
- [POJ3612] Telephone Wire(暴力dp+剪枝)
[POJ3612] Telephone Wire(暴力dp+剪枝) 题面 有N根电线杆,初始高度为h[i],要给相邻的两根连线.可以选择拔高其中一部分电线杆,把一根电线杆拔高\(\Delta H\)的 ...
- CodeForces 742B Arpa’s obvious problem and Mehrdad’s terrible solution (暴力枚举)
题意:求定 n 个数,求有多少对数满足,ai^bi = x. 析:暴力枚举就行,n的复杂度. 代码如下: #pragma comment(linker, "/STACK:1024000000 ...
- 2014牡丹江网络赛ZOJPretty Poem(暴力枚举)
/* 将给定的一个字符串分解成ABABA 或者 ABABCAB的形式! 思路:暴力枚举A, B, C串! */ 1 #include<iostream> #include<cstri ...
- HNU 12886 Cracking the Safe(暴力枚举)
题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12886&courseid=274 解题报告:输入4个数 ...
- 51nod 1116 K进制下的大数 (暴力枚举)
题目链接 题意:中文题. 题解:暴力枚举. #include <iostream> #include <cstring> using namespace std; ; ; ch ...
随机推荐
- 不修改系统日期和时间格式,解决Delphi报错提示 '****-**-**'is not a valid date and time
假如操作系统的日期格式不是yyyy-MM-dd格式,而是用strtodate('2014-10-01')) 来转换的话,程序会提示爆粗 '****-**-**'is not a valid date ...
- c++ primer 5 表达式
简单总结下容易忽视的地方和易错点吧 1 常用的位操作符,leecode很多算法题都是靠位运算解决的 2 箭头操作符 -> 等价于(* ). 对指针的成员操作 3 sizeof操作符 对 c ...
- VMware虚拟机VMware Authorization Service不能启动问题
出现VMware Authorization Service不能启动问题,注意要在安装VMware Player时使用管理员权限
- 【转】python+django+vue搭建前后端分离项目
https://www.cnblogs.com/zhixi/p/9996832.html 以前一直是做基于PHP或JAVA的前后端分离开发,最近跟着python风搭建了一个基于django的前后端分享 ...
- CodeForces 772A Voltage Keepsake
二分答案,验证. 二分到一个答案,比他小的时间都需要补充到这个时间,计算所需的量,然后和能提供的量进行比较. #include <cstdio> #include <cmath> ...
- Javascript 中的神器
Promise in js 回调函数真正的问题在于他剥夺了我们使用 return 和 throw 这些关键字的能力.而 Promise 很好地解决了这一切. 2015 年 6 月,ECMAScript ...
- android studio 汉化 美化 个性化 修改 安卓工作室 2.3.3 最新版
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 先看一下效果. 建议全屏看图,或者新标签看图.
- BZOJ 3676: [Apio2014]回文串 后缀自动机 Manacher 倍增
http://www.lydsy.com/JudgeOnline/problem.php?id=3676 过程很艰难了,第一次提交Manacher忘了更新p数组,超时,第二次是倍增的第0维直接在自动机 ...
- Test20171009 考试总结 NOIP模拟赛
题目难度合适,区分度适中,但是本人水平不佳,没有拿到满意的分数. T1(matrix) 一种比较容易想到的想法是枚举起点求出最长全1串做预处理,这是O(n^2)的. 接着枚举列起点,列终点,通过后缀和 ...
- 初涉springboot(一)
概述 1.了解springboot的作用 2.构建第一个springboot项目 一.springboot的作用 ① 原先在构建SSM项目的时候,可以感觉到,在一些不是很大的项目,构建配置文件的过程所 ...