题目描述

一个等差数列是一个能表示成a, a+b, a+2b,..., a+nb (n=0,1,2,3,...)的数列。

在这个问题中a是一个非负的整数,b是正整数。写一个程序来找出在双平方数集合(双平方数集合是所有能表示成p的平方 + q的平方的数的集合,其中p和q为非负整数)S中长度为n的等差数列。

输入输出格式

输入格式:

第一行: N(3<= N<=25),要找的等差数列的长度。

第二行: M(1<= M<=250),搜索双平方数的上界0 <= p,q <= M。

输出格式:

如果没有找到数列,输出`NONE'。

如果找到了,输出一行或多行, 每行由二个整数组成:a,b。

这些行应该先按b排序再按a排序。

所求的等差数列将不会多于10,000个。

输入输出样例

输入样例#1:

5
7
输出样例#1:

1 4
37 4
2 8
29 8
1 12
5 12
13 12
17 12
5 20
2 24

说明

题目翻译来自NOCOW。

USACO Training Section 1.4

枚举差值,注意优化

#include<cstdio>
#include<algorithm>
using namespace std;
int n,m;
bool can[];
int f[];
int cnt=;
struct node{
int a,b;
}ans[];
bool cmp(node a,node b)
{
if(a.b>b.b)return ;
if(a.a>b.a)return ;
return ;
}
int main()
{
int num=;
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
for(int j=i;j<=m;j++)
{
if(!can[i*i+j*j])
{
can[i*i+j*j]=;
f[++num]=i*i+j*j;
}
}
}
sort(f+,f+num+);
for(int i=;i<=num-n+;i++)
{
int a=f[i],j=i;
bool flag=;
while()
{ j++;
int tmp=f[j]-f[i];
if(j>num-n+) break;
if(a+tmp*(n-)>f[num])break;
for(int q=;q<n;q++)
{
if(!can[a+q*tmp])
{
flag=;break;
}
}
if(flag)break;
ans[++cnt].a=a;
ans[cnt].b= tmp;
}
}
sort(ans+,ans+cnt+,cmp);
if(!cnt)
printf("NONE");
else for(int i=;i<=cnt;i++)
{
printf("%d %d\n",ans[i].a,ans[i].b);
}
return ;
}

luogu P1214 [USACO1.4]等差数列 Arithmetic Progressions的更多相关文章

  1. 洛谷P1214 [USACO1.4]等差数列 Arithmetic Progressions

    P1214 [USACO1.4]等差数列 Arithmetic Progressions• o 156通过o 463提交• 题目提供者该用户不存在• 标签USACO• 难度普及+/提高 提交 讨论 题 ...

  2. [USACO1.4]等差数列 Arithmetic Progressions

    题目描述 一个等差数列是一个能表示成a, a+b, a+2b,..., a+nb (n=0,1,2,3,...)的数列. 在这个问题中a是一个非负的整数,b是正整数.写一个程序来找出在双平方数集合(双 ...

  3. 等差数列Arithmetic Progressions题解(USACO1.4)

    Arithmetic Progressions USACO1.4 An arithmetic progression is a sequence of the form a, a+b, a+2b, . ...

  4. poj 3006 Dirichlet's Theorem on Arithmetic Progressions【素数问题】

    题目地址:http://poj.org/problem?id=3006 刷了好多水题,来找回状态...... Dirichlet's Theorem on Arithmetic Progression ...

  5. USACO 1.4 Arithmetic Progressions

    Arithmetic Progressions An arithmetic progression is a sequence of the form a, a+b, a+2b, ..., a+nb ...

  6. [Educational Codeforces Round 16]D. Two Arithmetic Progressions

    [Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...

  7. 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 ...

  8. POJ 3006 Dirichlet's Theorem on Arithmetic Progressions (素数)

    Dirichlet's Theorem on Arithmetic Progressions Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  9. (素数求解)I - Dirichlet&#39;s Theorem on Arithmetic Progressions(1.5.5)

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit cid=1006#sta ...

随机推荐

  1. Eclipse主题更换方法

    1.打开Eclipse的Help->Eclipse Marketplace 2.在Find里搜索Eclipse Color Theme,点击Install按钮 3.打开Window->Pr ...

  2. vrpie在Visio Studio 中无法调试的问题

    最近这这几天一直在研究vrpie,之前不能调试,一调试就出问题,打开那个生成的htm文件是没问题的,最初的解决方法是不通过调试来打可那个htm页面,但是这样比较麻烦,因为经常需要和服务器交互,就只能用 ...

  3. Algorithms(fourth edition)——无向图

    1.设计图基本操作API 2.用什么数据结构来表示图并实现API 要求:(1)要预留足够空间 (2)实例方法实现要快 三个选择: 邻接矩阵:布尔矩阵,不满足条件一,而且无法表示平行边 边的数组:不满足 ...

  4. 安装 Windows Server 2012 Active Directory 只读域控制器 (RODC)(级别 200)

    安装 Windows Server 2012 Active Directory 只读域控制器 (RODC)(级别 200) 适用对象:Windows Server 2012 本主题介绍如何创建分步的 ...

  5. 47.关于gradle的解疑

    Short Answer Gradle is a build system. Long Answer Before Android Studio you were using Eclipse for ...

  6. 【Gas Station】cpp

    题目: There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. ...

  7. MySQL出现错误1045,用户无法连接

    1.root用户可以登陆进去 假设新建了个sqlin用户,但是无法用此用户连接MySQL 这个时候root用户可以登录吧,root用户登录进去,重新修改该用户的密码,执行语句 use mysql; u ...

  8. 【转】Unity3d的单例及场景间的数据传递

    http://blog.csdn.net/zy19940906/article/details/47724387  单例是场景间切换时传递数据的最常见的方式之一,在unity中,很多方法被封装,有时候 ...

  9. socket传输对象

    server public class Server{ private static int port = 8888; private static ServerSocket serverSocket ...

  10. SolrCloud在linux上的搭建

    SolrCloud在linux上的搭建 1.环境准备 三台虚拟机的环境准备: 1. 更改主机名 2. 关闭selinux 3. 关闭防火墙 4. 更改主机名与ip地址的映射 5. 时钟同步 6. ss ...