题目描述

一个等差数列是一个能表示成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. Not a git repository (or any of the parent directories): .git解决

    首先git init .然后在执行就行了.意思应该是当前目录不是git.

  2. 洛谷P1424小鱼的航程改进版

    题目链接https://www.luogu.org/problemnew/show/P1424

  3. 11、JQuery知识点总结

    1.JQuery简介 JQuery 是一套跨浏览器的JavaScript库,简化HTML与JavaScript之间的操作 jQuery有下列特色: 跨浏览器的DOM元素选择 DOM巡访与更改:支持CS ...

  4. Bug的类型

    美国计算机科学家.图灵奖获得者詹姆斯·尼古拉·格雷(Jim Gray),在他的著名的论文“Why do computers stop and what can be done about it?”中首 ...

  5. log4j2用asyncRoot配置异步日志是如何使用disruptor

    用asyncRoot配置对应的对接disruptor类是AsyncLoggerConfigDisruptor,用Log4jContextSelector启动参数配置全局异步的对应的对接disrupto ...

  6. xss games20关小游戏附源代码

    1. get方式的的值直接输出来了. ?name=<script>alert(1)</script> 2. 同样没有过滤,不过需要闭合前边的双引号和>. "&g ...

  7. PHP 命名空间和自动加载

    PHP 命名空间 php5.3 之后引入了命名空间的特性,从本质上讲,命名空间就是一个容器,你可以将类.函数和变量放在其中,在命名空间中,你可以无条件地访问这些项,在命名空间之外,必须导入或引用命名空 ...

  8. Hibernate命名策略及配置

    hibernate 表 命名策略         分类:            hibernate2013-02-27 18:46464人阅读评论(0)收藏举报 Hibernate注释下的自定义架构实 ...

  9. 编译php 转

    LAOGAO added 20140907: configure: error: mcrypt.h not found. Please reinstall libmcrypt.复制代码 代码如下:wg ...

  10. 【bzoj2245】[SDOI2011]工作安排 费用流

    题目描述 你的公司接到了一批订单.订单要求你的公司提供n类产品,产品被编号为1~n,其中第i类产品共需要Ci件.公司共有m名员工,员工被编号为1~m员工能够制造的产品种类有所区别.一件产品必须完整地由 ...