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 ...
随机推荐
- 用递归法计算从n个人中选选k个人组成一个委员会的不同组合数
用递归法计算从n个人中选选k个人组成一个委员会的不同组合数. 分析 由n个人里选k个人的组合数= 由n-1个人里选k个人的组合数+由n-1个人里选k-1个人的组合数: 当n = k或k = 0时,组合 ...
- 安装requests
requests包让Python程序能够轻松地向网站请求信息以及检查返回的响应.要安装requests,请执行类似于下面的命令: $ pip3 install --user requests http ...
- Good Bye 2015 F - New Year and Cleaning
F - New Year and Cleaning 这题简直是丧心病狂折磨王.. 思路:容易想到这样一个转换,把整个矩形一起移动,矩形移出去的时候相当于一行或者一列. 为了优化找到下一个消去的点,我先 ...
- Django总叙(转)
Django 千锋培训读书笔记 https://www.bilibili.com/video/av17879644/?p=1 切换到创建项目的目录 cd C:\Users\admin\Desktop\ ...
- ThinkPHP 多语言的实现
1.按照官方文档进行修改 2.注意区分项目语言包和系统语言包 3.实现语言包和数据库语言同步切换 4.thinkPHP多语言实现与Cookie有关, 谷歌浏览器下按F12查看Request Heade ...
- ubuntu下 pycharm使用andcoda下的tensorflow
在ubuntu中 tensorflow是安装在andconda里的一个虚拟环境中,他就相当于一个容器,将tensorflow的整个环境去模拟隔离出来,因为我们直接使用andconda库去作为pycha ...
- CodeForces 785C Anton and Fairy Tale
二分. 如果$n≤m$,显然只能$n$天. 如果$n>m$,至少可以$m$天,剩余还可以支撑多少天,可以二分计算得到,也可以推公式.二分计算的话可能爆$long$ $long$,上了个$Java ...
- Array.reduce()方法的使用
起因是学习异步函数的串行与并行写法时,发现reduce方法可以简化写法,然后看到一篇博客里面这样一段代码: var array = [1, [2, [3, 4], 5], 6]; function f ...
- 【BZOJ 2299】 2299: [HAOI2011]向量 (乱搞)
2299: [HAOI2011]向量 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 1255 Solved: 575 Description 给你一 ...
- 【BZOJ 1923】1923: [Sdoi2010]外星千足虫 (高斯消元异或 | BITSET用法)
1923: [Sdoi2010]外星千足虫 Description Input 第一行是两个正整数 N, M. 接下来 M行,按顺序给出 Charles 这M次使用“点足机”的统计结果.每行 包含一个 ...