[USACO1.4]等差数列Arithmetic Progressions 枚举
来源:互联网 发布:眼鼻嘴网络歌手翻唱 编辑:程序博客网 时间:2024/05/21 01:31
[USACO1.4]等差数列Arithmetic Progressions
题目描述
一个等差数列是一个能表示成a, a+b,a+2b,..., a+nb (n=0,1,2,3,...)的数列。
在这个问题中a是一个非负的整数,b是正整数。写一个程序来找出在双平方数集合(双平方数集合是所有能表示成p的平方 + q的平方的数的集合,其中p和q为非负整数)S中长度为n的等差数列。
输入输出格式
输入格式:
第一行:N(3<= N<=25),要找的等差数列的长度。
第二行:M(1<= M<=250),搜索双平方数的上界0 <= p,q <= M。
输出格式:
如果没有找到数列,输出`NONE'。
如果找到了,输出一行或多行, 每行由二个整数组成:a,b。
这些行应该先按b排序再按a排序。
所求的等差数列将不会多于10,000个。
输入输出样例
输入样例#1:
5
7
输出样例#1:
1 4
37 4
2 8
29 8
1 12
5 12
13 12
17 12
5 20
2 24
枚举前两个数,找一下即可。
#include<iostream>#include<algorithm>#include<cstring>#include<cstdio>#define lim 250*250*2using namespace std;struct haha{int x,y;}res[101000];bool f[lim+10];int a[101000];int n,m;bool cmp(haha p,haha q){return p.y==q.y?p.x<q.x:p.y<q.y;}bool judge(int a,int b){ int cur=1; while(cur<n) { a+=b; if (f[a]) cur++; else return false; } return true;}int main(){ memset(f,0,sizeof(f)); cin>>n>>m;int mm=2*m*m; for (int p=0;p<=m;p++) for (int q=0;q<=m;q++) f[p*p+q*q]=1; int cnt=0,tot=0; for (int i=0;i<=lim;i++) if (f[i]) a[cnt++]=i; for (int i=0;i<cnt;i++) for (int j=i+1;j<cnt;j++) { int cha=a[j]-a[i]; if (a[j]+(n-2)*cha>mm) break;//常数优化也是优化,不加优化就会T,qwq。 if(judge(a[i],cha)) res[tot].x=a[i],res[tot++].y=cha; } sort(res,res+tot,cmp); if (!tot) puts("NONE"); else for (int i=0;i<tot;i++) cout<<res[i].x<<' '<<res[i].y<<endl; return 0;}
阅读全文
0 0
- [USACO1.4]等差数列Arithmetic Progressions 枚举
- [USACO1.4]等差数列 Arithmetic Progressions
- USACO1.4 等差数列 Arithmetic Progressions
- [USACO1.4]等差数列 Arithmetic Progressions
- P1214 [USACO1.4]等差数列 Arithmetic Progressions
- 洛谷 P1214 [USACO1.4] 等差数列 Arithmetic Progressions
- 【数论】洛谷 P1214 [USACO1.4]等差数列 Arithmetic Progressions
- 【模拟枚举】Arithmetic Progressions等差数列(Usaco_Training 1.4)
- usaco1.4.1 Arithmetic Progressions
- Arithmetic Progressions等差数列
- USACO1.4.1 Arithmetic Progressions (ariprog)
- USACO1.4.1 Arithmetic Progressions (ariprog)
- 洛谷1214 等差数列 Arithmetic Progressions
- USACO-Section1.4 Arithmetic Progressions【暴力枚举】
- USACO-Section1.4 Arithmetic Progressions [枚举]
- [U]Arithmetic Progressions 枚举
- Usaco 1.4.3 等差数列(Arithmetic Progressions)
- USACO:1.4.3 Arithmetic Progressions 等差数列 解析
- samba案例及解析
- 页面滚动到指定位置导航栏固定顶部
- 视觉slam14讲——第9讲 设计前端
- JavaScript的bom对象
- Linux下Redis的安装和部署
- [USACO1.4]等差数列Arithmetic Progressions 枚举
- 获取对象属性的点方法和中括号法的区别
- SRS 代码分析
- 转载1
- logging模块学习笔记
- 内网穿透工具之ngrok
- Android实现二维码扫描库
- 【android逆向笔记】(四)火柴人内购逆向破解
- 排序---快速排序