USACO Arithmetic Progressions(暴力)
题目请点我
题解:
这道题的题意是找出集合里全部固定长度为N的等差数列。集合内的元素均为P^2+q^2的形式(0<=p,q<=M)。时间要求5s内。本着KISS,直接暴力。
可是后来竟超时了。检查后发现是map的问题,本想利用map实现常数级的查找。可是显然map内部不是这种。所以对于普通的数据类型。数据量不大(250^2+250^2)的情况下还是利用数组标记查找好一点,get。
代码实现:
/*
ID: eashion
LANG: C++
TASK: ariprog
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <map>
#include <algorithm>
#define MAX 125000
using namespace std;
int N,M;
bool flag;
int num[MAX];
int mm[MAX];
bool test(int start,int len);
int main()
{
freopen("ariprog.in","r",stdin);
freopen("ariprog.out","w",stdout);
while( scanf("%d%d",&N,&M) != EOF ){
int pos = 0;
flag = false;
memset(mm,0,sizeof(mm));
for( int i = 0; i <= M; i++ ){
for( int j = 0; j <= M; j++ ){
int tmp = i*i+j*j;
if( mm[tmp] != 1 ){
num[pos] = tmp;
mm[tmp] = 1;
pos++;
}
}
}
sort(num,num+pos);
int up_B = (num[pos-1]-num[0])/(N-1);
for( int i = 1; i <= up_B; i++ ){
for( int j = 0; j < pos; j++ ){
if( test(num[j],i) ){
flag = true;
printf("%d %d\n",num[j],i);
}
if( num[j]+(N-1)*i > num[pos-1] ){
break;
}
}
}
if( flag == false ){
printf("NONE\n");
}
}
return 0;
}
bool test(int start,int len){
int tmp = start;
for( int i = 0; i < N; i++ ){
if( mm[tmp] != 1 ){
return false;
}
tmp += len;
}
return true;
}
USACO Arithmetic Progressions(暴力)的更多相关文章
- USACO Arithmetic Progressions 【构造等差数列】
USER: Jeremy Wu [wushuai2] TASK: ariprog LANG: C++ Compiling... Compile: OK Executing... Test 1: TES ...
- USACO 1.4 Arithmetic Progressions
Arithmetic Progressions An arithmetic progression is a sequence of the form a, a+b, a+2b, ..., a+nb ...
- 洛谷P1214 [USACO1.4]等差数列 Arithmetic Progressions
P1214 [USACO1.4]等差数列 Arithmetic Progressions• o 156通过o 463提交• 题目提供者该用户不存在• 标签USACO• 难度普及+/提高 提交 讨论 题 ...
- E - Two Arithmetic Progressions(CodeForces - 710D)(拓展中国剩余定理)
You are given two arithmetic progressions: a1k + b1 and a2l + b2. Find the number of integers x such ...
- [Educational Codeforces Round 16]D. Two Arithmetic Progressions
[Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...
- Dirichlet's Theorem on Arithmetic Progressions 分类: POJ 2015-06-12 21:07 7人阅读 评论(0) 收藏
Dirichlet's Theorem on Arithmetic Progressions Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ 3006 Dirichlet's Theorem on Arithmetic Progressions (素数)
Dirichlet's Theorem on Arithmetic Progressions Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- poj 3006 Dirichlet's Theorem on Arithmetic Progressions【素数问题】
题目地址:http://poj.org/problem?id=3006 刷了好多水题,来找回状态...... Dirichlet's Theorem on Arithmetic Progression ...
- (素数求解)I - Dirichlet's Theorem on Arithmetic Progressions(1.5.5)
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit cid=1006#sta ...
随机推荐
- ASP.Net中关于WebAPI与Ajax进行跨域数据交互时Cookies数据的传递
本文主要介绍了ASP.Net WebAPI与Ajax进行跨域数据交互时Cookies数据传递的相关知识.具有很好的参考价值.下面跟着小编一起来看下吧 前言 最近公司项目进行架构调整,由原来的三层架构改 ...
- Python yield使用
https://www.ibm.com/developerworks/cn/opensource/os-cn-python-yield/ 您可能听说过,带有 yield 的函数在 Python 中被称 ...
- 网站性能工具-YSlow的23个规则-网站性能优化
1. 减少HTTP请求次数 合并图片.CSS.JS,改进首次访问用户等待时间. 2. 使用CDN 就近缓存==>智能路由==>负载均衡==>WSA全站动态加速 3. 避免空的src和 ...
- leetcode第一刷_Unique Binary Search Trees
这道题事实上跟二叉搜索树没有什么关系,给定n个节点,让你求有多少棵二叉树也是全然一样的做法.思想是什么呢,给定一个节点数x.求f(x),f(x)跟什么有关系呢,当然是跟他的左右子树都有关系.所以能够利 ...
- android音乐播放器开发 SweetMusicPlayer 摇一摇换歌
上一篇写了怎样在线匹配歌词,http://blog.csdn.net/huweigoodboy/article/details/39878063,如今来讲讲摇一摇功能开发. 相同用了一个Service ...
- iOS objc_msgSend 报错解决方案
错误代码: objc_msgSend(self.beginRefreshingTaget, self.beginRefreshingAction, self); Too many arguments ...
- 使用 ssh -R 建立反向/远程TCP端口转发代理
转自:https://yq.aliyun.com/articles/8469 ssh是一个非常棒的工具, 不但能建立动态转发, 例如chrome的Switchy插件用到的就是这个技术.http://b ...
- Spring Mvc如何通过注解的方式设置视图解析器的优先级
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalRes ...
- 不规则形状的Mask动画
不规则形状的Mask动画 效果 源码 https://github.com/YouXianMing/Animations // // MaskShapeViewController.m // Anim ...
- command line subversion for windows
安装 Slik SVN 之后,进入dos, 输入svn help,所有的命令和相关的信息都出来了 相关的svn命令:log.export等,可以搜索相应的文章: svn常用命令 svn log - ...