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, . ...
随机推荐
- WordPress瀑布流主题PinThis中文版v1.6.8
PinThis主题来源于英语网站http://pinthis.pixelbeautify.com/的汉化(语言文件+控制面板),中文版采用的是翻译器手工核对,并不完美,只对主题中文化,其他没做任何更改 ...
- parseint和isNaN用法
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- poj3281 Dining 最大流(奇妙的构图)
我是按照图论500题的文档来刷题的,看了这题怎么也不觉得这是最大流的题目.这应该是题目做得太少的缘故. 什么是最大流问题?最大流有什么特点? 最大流的特点我觉得有一下几点: 1.只有一个起点.一个终点 ...
- okhttp3 ExceptionInInitializerError 异常处理
okhttp3 在Android4.4上出现下面异常 java.lang.ExceptionInInitializerError at okhttp3.OkHttpClient.newSslSocke ...
- Module, Package in Python
1.To put it simple, Module是写好的一系列函数或变量,文件以.py为后缀,可以在其他Module中整体或部分引用. PS: 在Module中[结尾或开头]加入if __name ...
- FBX骨骼坐标系与模型坐标系的关系
采用assimp加载FBX文件.首先记录下ubuntu下assimp的编译安装. cd assimp_unzip_dir mkdir build cd build && cmake . ...
- AI:**消灭程序员需要一百年吗?
这篇博文真的很长,不过挺有意思.关于智能机器人的发展前景还是很广的,因为每一步都异常艰难,而什么时候可以终止还无法预料,所以程序员没办法失业啊! 转自于图灵社区:http://www.ituring. ...
- 考考你对java多态的理解
请看如下代码, 如果你能不运行得出正确答案, 那你真的超过99%的java程序员了. [本人属于最大头的那部分] public class A{ public String s = "A&q ...
- CorelDRAW X6、X7&2018,500现金返利等你拿!
购物狂欢节要来了,你准备好了么? 不知何时起,四根神棍的日子却成了大家拼爹.拼钱包.拼手速.拼网速.拼钱包...各种火拼日子 你是从哪年关注并重视双11的,记得小编我第一次邂逅双11真的只是凑凑热闹 ...
- <td colspan="6"></td>代表这个td占6个td的位置
<td colspan="6"><span class="order-time">2017-10-11 14:55:23</spa ...