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, . ...
随机推荐
- JS 经验总结
1.IE中div的高度是content+padding+border之和,其它的是content的高度 2.一个标签里面只有一个属性,class='cls1 cls2' 3.同一页面不能出现相同的id ...
- DataTable转Json就是这么简单(Json.Net DLL (Newtonsoft))
之前JSON转DataTable可以见我之前的随笔 链接Json转换成DataTable 之前没有用过DataTable,之后随着需求的叠加发现需要将DataTable转换成Json.因为之前都是用的 ...
- javascript中标准事件流addEventListener介绍
addEventListener-开始 前面零散地写了些关于 addEventListener 的内容,觉得比较散,有些地方可能也说得不够清楚明白,所以决定以连载的形式从头到尾再写一篇. addEve ...
- DB2高可用hadr搭建参数配置
主机上执行db2 UPDATE DB CFG FOR XMHIS USING HADR_LOCAL_HOST 192.168.1.2db2 UPDATE DB CFG FOR XMHIS USING ...
- Python笔记22-----高阶函数
1.sorted(排序对象,key=):排序对象可以是类别,也可以是字符串和字典,key为自定义排序,如:[key=abs,按绝对值排序][key=lambda x:x[1],按排序对象的第二个值排序 ...
- GDI Bezier 样条曲线(7)
Bezier 样条曲线 Bezier 样条曲线使用四个点来定义:两个端点(起点和终点)和两个控点(用于使其不同程度地弯曲). 绘制 Bezier 样条曲线 使用 PolyBezier 函数和 Poly ...
- vue组件通信,点击传值,动态传值(父传子,子传父)
转载:https://blog.csdn.net/xr510002594/article/details/83304141 一.父组件传子组件,核心--props 在这里触发 handleClick ...
- 微信小程序 全局变量
微信小程序里面有个app.js,我们可以在这个里面设置全局变量, App({ globalData:{ url:"http://xxx.xxx.xx:3000" } }) 在外面就 ...
- jQuery练习总结(一)
第一个jQuery程序: 注意:使用jQuery时候定义单击事件使用的方法是click(function(){处理单击事件产生时所要执行的代码}) , 而JavaScript定义的单击事件则是 xx. ...
- 模仿学习小游戏外星人入侵-Python学习,体会“函数”编程
游戏类如下: # !/usr/bin/python # -*- coding:utf-8 -*- """ Author :ZFH File :alien.py Softw ...