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 ...
随机推荐
- Codeforces 235E. Number Challenge DP
dp(a,b,c,p) = sigma ( dp(a/p^i,b/p^j,c/p^k) * ( 1+i+j+k) ) 表示用小于等于p的素数去分解的结果有多少个 E. Number Challenge ...
- PHP 7 来了,PHP 6 去哪儿了?
PHP7来了,那么PHP6去哪儿了呢? PHP7简介 PHP7是PHP编程语言全新的一个版本,主要在性能方面获得了极大的提升.官方的文档显示,PHP7可以达到PHP5.x版本两倍的性能.同时还 对PH ...
- 无法打开物理文件 XXX.mdf",操作系统错误 5:"5(拒绝访问。)"的解决办法
http://blog.csdn.net/blackfield/article/details/6550499 用T-SQL命令附加数据库时,出现如下异常信息: 无法打开物理文件 XXX.mdf&qu ...
- Windows Phone本地数据库(SQLCE):11、使用LINQ查询数据库(翻译) (转)
这是“windows phone mango本地数据库(sqlce)”系列短片文章的第十一篇. 为了让你开始在Windows Phone Mango中使用数据库,这一系列短片文章将覆盖所有你需要知道的 ...
- Java知识回顾 (2) Java 修饰符
一.Java 修饰符 1.1 访问控制修饰符 Java中,可以使用访问控制符来保护对类.变量.方法和构造方法的访问.Java 支持 4 种不同的访问权限. default (即缺省,什么也不写): 在 ...
- UITableViewCell图片视差效果
UITableViewCell图片视差效果 效果 源码 https://github.com/YouXianMing/Animations 细节 OffsetImageCell.m OffsetCel ...
- java.lang.ThreadLocal类
一.概述 ThreadLocal是什么呢?其实ThreadLocal并非是一个线程的本地实现版本,它并不是一个Thread,而是threadlocalvariable(线程局部变量).也 ...
- 学了编译原理能否用 Java 写一个编译器或解释器?
16 个回答 默认排序 RednaxelaFX JavaScript.编译原理.编程 等 7 个话题的优秀回答者 282 人赞同了该回答 能.我一开始学编译原理的时候就是用Java写了好多小编译器和 ...
- 部署Percona XtraDB Cluster高可用和多Master集群
http://www.it165.net/admin/html/201401/2306.html http://www.oschina.net/p/percona-xtradb-cluster/ ht ...
- [转]php的public、protected、private三种访问控制模式的区别
FROM : http://blog.163.com/weiwenjuan_bj/blog/static/14035033620129304183850/?suggestedreading publi ...