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, . ...
随机推荐
- 3.ThinkPHP入门---视图
视图:MVC三大组成部分,负责信息的展示和输出 1.视图的创建 创建的位置需要是在分组目录下的view目录下余控制器同名的目录中. 2.视图的展示 在smarty和tinkphp都是使用diaplay ...
- merge into语句的使用
merge语法是根据源表对目标表进行匹配查询,匹配成功时更新,不成功时插入. 其基本语法规则是 merge into 目标表 a using 源表 b on(a.条件字段1=b.条件字段1 and a ...
- 外观模式(Facade)C++实现
外观模式 意图: 为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一系统更加容易使用. 适用性: 1.在设计初期阶段,应该要有意识的将不同的两个层分离,比如经典的三层架 ...
- showdialog
在C#中窗口的显示有两种方式:模态显示(showdialog)和非模态显示(show). 区别: 模态与非模态窗体的主要区别是窗体显示的时候是否可以操作其他窗体.模态窗体不允许操作其他窗体,非模态窗体 ...
- CI中的验证码
CI中的验证码相对来说使用非常方便直接加载类调用函数以及一些配置,代码如上,比较简单,具体函数可在CI手册的辅助函数参考CAPTCHA辅助函数中查询,CI中的验证码是直接生成验证码图片在你自己创建的C ...
- Nginx代码风格图示
Nginx代码风格图示 (100%) 一.基本原则 K&R编码风格(偏BSD子类). 每行不能超过80列. 不用TAB对齐,用空格. 默认对齐单元是4个空格. 除宏定义外,字母均为小写,单词间 ...
- 关于table排版
colspan和rowspan这两个属性用于创建特殊的表格. colspan是“column span(跨列)”的缩写.colspan属性用在td标签中,用来指定单元格横向跨越的列数: 在浏览器中 ...
- RabbitMQ学习之集群模式
由于RabbitMQ是用erlang开发的,RabbitMQ完全依赖Erlang的Cluster,因为erlang天生就是一门分布式语言,集群非常方便,但其本身并不支持负载均衡.Erlang的集群中各 ...
- IOS开发:官方自带的JSON使用
apple在ios5的时候提供了json的相关处理api,其中最主要的是NSJSONSerialization类. + (BOOL)isValidJSONObject:(id)obj; + (NSDa ...
- maven构建本地jar包到本地仓库
maven命令: mvn:install:install-file -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=1.0 -Dpackaging ...