题目描述

一个等差数列是一个能表示成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. 如何提高STM32的学习效率

    时间如何安排 做任何事情前,习惯写一个计划——要在一个月内上手STM32! 没有计划的日子,每天早上醒来睁开眼睛,却不知道自己今天要干啥 计划和时间安排: 第一阶段:找感觉——谈及STM32,立即反应 ...

  2. CSS需要注意的问题1(转生活因拼搏而精彩的网易博客)

      1.检查HTML元素(如:<ul>.<div>).属性(如:class=”")是否有拼写错误.是否忘记结束标记(如:<br />) 因为Xhtml 语 ...

  3. Python 技巧(三)—— list 删除一个元素的三种做法

    我们以一个字符串为元素类型的 list 为例,进行列表元素的删除: >>> l = ['no surfing', 'flippers'] 1 法一:remove(val) >& ...

  4. 【Keepalived+MySQL】MySQL双主互备+高可用

    一.基本信息说明 [DB1] IP: 192.168.102.144 hostname: LVS-Real1 [DB2] IP: 192.168.102.145 hostname: LVS-Real2 ...

  5. mac攻略(八) -- 神器zsh和iterm2的配置

      1. 安装oh my zsh 安装命令: curl -L http://install.ohmyz.sh | sh 修改shell的方式: chsh -s /bin/zsh   2.安装cask( ...

  6. 设计模式之第4章-装饰模式(Java实现)

    设计模式之第4章-装饰模式(Java实现) “怎么了,鱼哥?” “唉,别提了,网购了一件衣服,结果发现和商家描述的差太多了,有色差就算了,质量还不好,质量不好就算了,竟然大小也不行,说好的3个X,邮的 ...

  7. python 学习分享-实战篇选课系统

    # 角色:学校.学员.课程.讲师 # 要求: # 1. 创建北京.上海 2 所学校 # 2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海开 # ...

  8. CSU-2221 假装是区间众数(ST表模版题)

    题目链接 题目 Description 给定一个非递减数列Ai,你只需要支持一个操作:求一段区间内出现最多的数字的出现次数. Input 第一行两个整数N,Q 接下来一行有N个整数,表示这个序列. 接 ...

  9. [oldboy-django][1初始django]后台管理页面的布局 + djano母版(继承html)

    完善学员管理系统 - bootstrap fontawesome - 分页,路径导航,表格(class样式),消息图标(i标签),邮件图标(i标签) - 响应式导航 @media(min-width, ...

  10. Unity 脚本<1>

    RaycastHit2D hit = Physics2D.Linecast(targetPosition, targetPosition + new Vector2(x, y)); 猜测是lineca ...