【模拟枚举】Arithmetic Progressions等差数列(Usaco_Training 1.4)

来源:互联网 发布:java 方法引用 编辑:程序博客网 时间:2024/05/10 07:10
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 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;}

 

 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 微信购物商城买的东西不发货怎么办 魅族手机电话图标没有了怎么办 新商盟网页新商盟网页打不开怎么办 京东商城购物车装满了怎么办 商家想入住美团没有营业执照怎么办 旅行团定好的人数临时加人该怎么办 淘宝上发货后12天未收到款怎么办 京东购买的东西烂了怎么办 苹果4s手机激活密码忘了怎么办 小米4c返回键失灵了怎么办 小米4c下面三个键失灵怎么办 谷歌浏览器打不开指定的网址怎么办 电脑上我的电脑图标没了怎么办 手机上的短信图标没了怎么办 qq密保手机被别人换了怎么办 苹果六手机很卡网络不给力怎么办 堡垒之夜卡在载入界面怎么办 登录新福建一直说网络不给力怎么办 开发游戏平台给了钱不给东西怎么办 代号英雄与服务器断开连接了怎么办 千牛聊天页面买家信息不显示怎么办 秒拍存草稿箱的视频没了怎么办? 登录山东掌厅出现服务器错误怎么办 微信号被多人投诉被限制登录怎么办 联想平板电脑开机密码忘记了怎么办 申请的qq没登录忘了账号怎么办 炫舞时代由于网络原因登不上怎么办 qq申请太多进不了热聊怎么办 手机号申请的微信号被盗了怎么办 买菜别人少找了钱不还怎么办 在掌上英雄联盟买皮肤买错区怎么办 win8我的电脑图标没了怎么办 英雄联盟老是卡在安全扫描怎么办 英雄联盟活动送皮肤没送怎么办 电脑换完系统有些页面打不开怎么办 王卡助手交手机费页面打不开怎么办 在浏览器上打不开路由器页面怎么办 英雄联盟读条的时候自动关机怎么办 手机的位置信息开不了怎么办呢 滴滴车主接到乘客返回路程要怎么办 移动换话费积分是发什么短信怎么办