USACO-Section1.4 Arithmetic Progressions[...]
来源:互联网 发布:电脑打字下载什么软件 编辑:程序博客网 时间:2024/05/21 06:30
2017-06-10
发现自己目前的最大问题就是浮躁。。根本无法静下心来思考……所谓欲速则不达。这个程序很好的提醒了我这个问题,,请不要慌张,无论面临什么处境。^_^
题目大意:
给出两个整数N (3 <= N <= 25),M(1 <= M <= 250)。定义p,q(0 <= p,q <= M),求由p^2+q^2构成的数列中长度不小于N的等差数列。输出的每行为一个符合条件的等差数列的首项和公差,输出顺序先按公差递增再按首项递增。
样例输入:
57
样例输出:
1 437 42 829 81 125 1213 1217 125 202 24
题解:
首先要求出p^2+q^2构成的序列,再在其中找等差数列。我用一个bool数组来记录可选值(p^2+q^2的可能值),这个数组的大小曾是bug之一。。至于公差的值应该在1~X,其中X为M^2/(N-1),这是0~M^2范围的整数可能构成长度不小于N的等差数列时,公差的最大取值。
此外,寻找等差数列时内循环的判断条件很重要,没有写全条件会运行超时。
代码如下:
/*ID: madara01PROG: ariprogLANG: C++*/#include <iostream>#include <fstream>#define MAX 250using namespace std;int squares[MAX + 1]; //存储0~250的平方bool bisquares[MAX*MAX*2 + 1]; //特别注意这个数组的大小。。int n,m;bool flag = false; //指示是否有解int main(){ ofstream fout ("ariprog.out"); ifstream fin ("ariprog.in"); fin >> n >> m; int i,j,k,x,count; squares[0] = 0; for(i = 1; i <= m; i++){ squares[i] = i*i; } int len = squares[m]*2 + 1; for(i = 0; i <= m; i++){ for(j = i; j <= m; j++){ k = squares[i] + squares[j]; //初始化可选值数组 bisquares[k] = true; } } k = len/(n-1) + 1; //公差最大值为(可选值最大值/(n-1)) for(i = 1; i < k; i++){ //公差的取值1~k for(j = 0; j < len ; j++){ //把每个可选值作为首项 if(!bisquares[j]) continue; x = j; count = 1; while(bisquares[x] == true && (x+i) < len && bisquares[x+i] == true && count <= n){ count++; x = x+i; } if(count >= n){ fout << j << " " << i << endl; flag = true; } } } if(flag == false) fout << "NONE" << endl; return 0;}
阅读全文
0 0
- USACO Section1.4 Arithmetic Progressions
- USACO-Section1.4 Arithmetic Progressions
- USACO-Section1.4 Arithmetic Progressions[...]
- USACO-Section1.4 Arithmetic Progressions
- USACO-Section1.4 Arithmetic Progressions
- USACO-Section1.4 Arithmetic Progressions【暴力枚举】
- USACO-Section1.4 Arithmetic Progressions [枚举]
- USACO-Section1.4 Arithmetic Progressions[其他][暴力枚举]
- usaco.section1.4.Arithmetic Progressions(枚举)
- USACO 1.4.4 Arithmetic Progressions
- usaco 1.4.4 Arithmetic Progressions
- USACO 1.4.4 Arithmetic Progressions
- USACO-cha1-sec1.4 Arithmetic Progressions
- USACO 1.4-Arithmetic Progressions
- Arithmetic Progressions(usaco)
- usaco PROB Arithmetic Progressions
- usaco Arithmetic Progressions
- USACO Arithmetic Progressions
- 栈的压入、弹出序列(java版)
- 图片垂直居中问题
- 梯度下降算法
- Haskell学习笔记 --- 理解 max :: (Ord a) => a -> (a -> a)
- 程序员必读书籍推荐
- USACO-Section1.4 Arithmetic Progressions[...]
- 通用RecyclerView和DataBinding
- Nginx配置文件(nginx.conf)配置详解
- Ceres Solver for android
- 手动安装m4, autoconf, automake, libtool
- SVN与Git区别
- java线程暂停与恢复suspend和resume
- cocoapods找不到第三方库Unable to find a pod with name, author, summary, or description matching 'AFNetwork'
- hdu 4858 项目管理 分块