USACO 1.4 Arithmetic Progressions
Arithmetic Progressions
An arithmetic progression is a sequence of the form a, a+b, a+2b, ..., a+nb where n=0,1,2,3,... . For this problem, a is a non-negative integer and b is a positive integer.
Write a program that finds all arithmetic progressions of length n in the set S of bisquares. The set of bisquares is defined as the set of all integers of the form p2 + q2 (where p and q are non-negative integers).
TIME LIMIT: 5 secs
PROGRAM NAME: ariprog
INPUT FORMAT
Line 1: | N (3 <= N <= 25), the length of progressions for which to search |
Line 2: | M (1 <= M <= 250), an upper bound to limit the search to the bisquares with 0 <= p,q <= M. |
SAMPLE INPUT (file ariprog.in)
5
7
OUTPUT FORMAT
If no sequence is found, a single line reading `NONE'. Otherwise, output one or more lines, each with two integers: the first element in a found sequence and the difference between consecutive elements in the same sequence. The lines should be ordered with smallest-difference sequences first and smallest starting number within those sequences first.
There will be no more than 10,000 sequences.
SAMPLE OUTPUT (file ariprog.out)
1 4
37 4
2 8
29 8
1 12
5 12
13 12
17 12
5 20
2 24 题目大意:给你n和m,n表示目标等差数列的长度(等差数列由一个非负的首项和一个正整数公差描述),m表示p,q的范围,目标等差数列的长度必须严格等于n且其中每个元素都得属于集合{x|x=p^2+q^2}(0<=p<=m,0<=q<=m),按顺序输出所有的目标数列。
思路:其实很简单,就是枚举,枚举起点和公差,一开始有点担心会超时,弄的自己神烦意乱的,但是实际上并没有。。。。。下面附上代码
/*
ID:fffgrdcc1
PROB:ariprog
LANG:C++
*/
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int a[*],cnt=,n,m;
int bo[];
struct str
{
int a;
int b;
}ans[];
int tot=;
bool check(int a,int b)
{
int temp=a+b+b,tt=m-;
while(tt--)
{
if(temp>n*n*||!bo[temp])return ;
temp+=b;
}
return ;
}
bool kong(str xx,str yy)
{
return xx.b<yy.b||(xx.b==yy.b&&xx.a<yy.a);
}
int main()
{
freopen("ariprog.in","r",stdin);
freopen("ariprog.out","w",stdout);
scanf("%d%d",&m,&n);
for(int i=;i<=n;i++)
{
for(int j=i;j<=n;j++)
{
bo[i*i+j*j]=;
}
}
for(int i=;i<=n*n*;i++)
if(bo[i])
a[cnt++]=i;
for(int i=;i<cnt;i++)
{
for(int j=i+;j<cnt;j++)
{
if(check(a[i],a[j]-a[i]))
{
ans[tot].a=a[i];
ans[tot++].b=a[j]-a[i];
}
}
}
sort(ans,ans+tot,kong);
if(!tot)printf("NONE\n");
for(int i=;i<tot;i++)
{
printf("%d %d\n",ans[i].a,ans[i].b);
}
return ;
}
USACO 1.4 Arithmetic Progressions的更多相关文章
- USACO Section1.4 Arithmetic Progressions 解题报告
ariprog解题报告 —— icedream61 博客园(转载请注明出处)-------------------------------------------------------------- ...
- 洛谷P1214 [USACO1.4]等差数列 Arithmetic Progressions
P1214 [USACO1.4]等差数列 Arithmetic Progressions• o 156通过o 463提交• 题目提供者该用户不存在• 标签USACO• 难度普及+/提高 提交 讨论 题 ...
- [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 ...
- Educational Codeforces Round 16 D. Two Arithmetic Progressions (不互质中国剩余定理)
Two Arithmetic Progressions 题目链接: http://codeforces.com/contest/710/problem/D Description You are gi ...
- 等差数列Arithmetic Progressions题解(USACO1.4)
Arithmetic Progressions USACO1.4 An arithmetic progression is a sequence of the form a, a+b, a+2b, . ...
随机推荐
- 如何正确产看API
看API时,先看的它的父接口自接口,及其相关的抽象类和子类 看完后,看概述的第一段话就行,后面的不用看. 再看构造方法,并到底层去看构造方法里参数的具体含义. 最后,再将包含的方法一个个进行测试. 解 ...
- CentOS6.5下nginx-1.8.1.tar.gz的单节点搭建(图文详解)
不多说,直接上干货! [hadoop@djt002 local]$ su root Password: [root@djt002 local]# ll total drwxr-xr-x. root r ...
- C#中的值类型、引用类型,代码告诉你他是什么类型。
C#代码告诉你这是什么类型. using System; using System.Collections.Generic; using System.Linq; using System.Text; ...
- 【正则表达式】从json数组中抽取id列表
有如下数组,要从中取出id: "[\"3812662409\",\"3812633637\",\"3812627686\",\&q ...
- 派遣函数IRP
派遣函数是Windows驱动程序中的重要概念.驱动程序的主要功能是负责处理I/O请求,其中大部分I/O请求是在派遣函数中处理的. 用户模式下所有对驱动程序的I/O请求,全部由操作系统转换为一个叫做IR ...
- 读书笔记6-浪潮之巅(part1)
浪潮之巅 ——对于一个人来讲,一生能够赶上一次科技革命的浪潮也就足够了 近一百多年来,总有一些公司很幸运地站在了技术革命的浪尖上.而一旦处在那个位置,就算只用随着潮流的发展而前行,也能安安稳稳地发展十 ...
- 继承&封装
扩展一个已有的类,并且继承该类的属性和行为这种方式成为继承. 实例 public class Polygon { public int sides; public double area; publi ...
- 「Redis 笔记」数据类型
REmote DIctionary Server(Redis),一个 key-value 存储系统. 数据类型 Redis 支持五种数据类型:string(字符串),hash(哈希),list(列表) ...
- (转)shiro权限框架详解03-shiro介绍
http://blog.csdn.net/facekbook/article/details/54893740 shiro介绍 本文正式进入主题.本文将介绍如下内容: 什么是shiro 为什么需要学习 ...
- JS去空格、截取页面url
1. 去掉字符串前后所有空格: 代码如下: function Trim(str) { return str.replace(/(^\s*)|(\s*$)/g, ""); } 说明 ...