题目描述

一个等差数列是一个能表示成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. [原]sencha touch之NavigationView

    还是直接上代码,都是基本的几个容器控件,没什么还说的 Ext.application({ name:'itkingApp', launch:function(){ var view =Ext.crea ...

  2. 4、CSS基础part-2

    1.background-1 ①设置background-image ②设置background-attachment为fixed 可以声明图像相对于可视区是固定的(fixed),因此不会受到滚动的影 ...

  3. IOS笔记049-UITabBarController

    1.简单实现 效果:在视图底部显示一个工具栏 代码实现 // 创建窗口 self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScre ...

  4. Python+Selenium练习篇之8-利用css定位元素

    前面介绍了,XPath, id , class , link text, partial link text, tag name, name 七大元素定位方法,本文介绍webdriver支持的最后一个 ...

  5. 第一章:前端布局之display属性

    css布局学习网站:http://zh.learnlayout.com

  6. git放弃修改&放弃增加文件

    1. 本地修改了一堆文件(并没有使用git add到暂存区),想放弃修改. 单个文件/文件夹: git checkout -- filename 所有文件/文件夹: git checkout . 2. ...

  7. 【数据结构与算法】Fibonacci Sequence

    学计算机的对 Fibonacci 都并不陌生,在课堂上一讲到递归几乎都会提到 Fibonacci 数列.不久前,我对 Fibonacci 产生了一些兴趣,就在这里把自己的想法给记录下来. 递推公式: ...

  8. [oldboy-django][2深入django]分页功能

    1 django自带分页 1.1 分页模板 <!DOCTYPE html> <html lang="en"> <head> <meta c ...

  9. 爬虫:Scrapy2 - 命令行工具

    Scrapy 是通过 scrapy 命令行工具进行控制的. 这里我们称之为 “Scrapy tool” 以用来和子命令进行区分.对于子命令,我们称为 “command” 或者 “Scrapy comm ...

  10. python 使用入的坑

    如测试代码,并没有将li.li_ 的交集查询出来 li=[1,2,3,4,5] li_=[2,5,6,7,9] for i in li_: if i in li: li_.remove(i) prin ...