【模拟枚举】Arithmetic Progressions等差数列(Usaco_Training 1.4)
来源:互联网 发布:java 方法引用 编辑:程序博客网 时间:2024/05/10 07:10
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 searchLine 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)
57
OUTPUT FORMAT
If no sequence is found, a singe 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 437 42 829 81 125 1213 1217 125 202 24
依次枚举a和b即可,最后由于按照b排序,所以可以把b的循环放在外层,也可以用一个优先队列存储
C++ Code
/*ID: jiangzh15TASK: ariprogLANG: C++http://blog.csdn.net/jiangzh7*/#include<cstdio>#include<utility>#include<queue>using namespace std;bool h[250*250*2+10];int n,m,maxv;bool check(int a,int b){ for(int i=0;i<n;i++) if(!h[a+i*b]) return false; return true;}int main(){ freopen("ariprog.in","r",stdin); freopen("ariprog.out","w",stdout); scanf("%d%d",&n,&m); int i,j; for(i=0;i<=m;i++) for(j=0;j<=m;j++) h[i*i+j*j]=true; maxv=m*m*2; bool flag=false; for(j=1;j<=maxv;j++)//由于按照b排序 所以b放在外层 for(i=0;i<=maxv;i++) { if(i+(n-1)*j>maxv)break; if(check(i,j)) { printf("%d %d\n",i,j); flag=true; } } if(!flag)printf("NONE\n"); return 0;}
- 【模拟枚举】Arithmetic Progressions等差数列(Usaco_Training 1.4)
- 【USACO题库】1.4.3 Arithmetic Progressions等差数列(模拟)
- [USACO1.4]等差数列Arithmetic Progressions 枚举
- Usaco 1.4.3 等差数列(Arithmetic Progressions)
- USACO:1.4.3 Arithmetic Progressions 等差数列 解析
- USACO 1.4 Arithmetic Progressions (等差数列)
- Arithmetic Progressions等差数列
- [USACO1.4]等差数列 Arithmetic Progressions
- USACO1.4 等差数列 Arithmetic Progressions
- [USACO1.4]等差数列 Arithmetic Progressions
- 洛谷1214 等差数列 Arithmetic Progressions
- 1251. 【USACO题库】1.4.3 Arithmetic Progressions等差数列
- USCAO section 1.4 Arithmetic Progressions(枚举)
- USACO-Section 1.4 Arithmetic Progressions(枚举)
- USACO 1.4 Arithmetic Progressions (堆排序 + 枚举)
- [U]Arithmetic Progressions 枚举
- P1214 [USACO1.4]等差数列 Arithmetic Progressions
- 洛谷 P1214 [USACO1.4] 等差数列 Arithmetic Progressions
- 【01背包】可爱迷人的香穗子
- java 运算符和表达式
- 【广搜】Cyh和香穗子
- 【动规递推】聪明伶俐的香穗子
- 【模拟递规】Mother's Milk 母亲的牛奶 (Usaco_Training 1.4)
- 【模拟枚举】Arithmetic Progressions等差数列(Usaco_Training 1.4)
- 【宽搜】The Clocks 时钟 (Usaco_Training 1.4)
- 驱动开发利器Microsoft Windows Driver Kit 7.1.0下载
- 【坐标型动态规划】Number Triangles数字金字塔(Usaco_Training 1.5)
- 【最大子区间和】旅游路线(travel.pas/c/cpp)
- Oracle Workflow Demo (2) - Function
- 【斐波拉契数列】悟空学艺(Monkey.pas/c/cpp)
- 如何使double为整数时不带小数点
- 【归并排序】序列(sequence.pas/c/cpp)