http://ace.delos.com/usacoprob2?a=WNyvQo2qbjE&S=ariprog

额。。。比较水的题,综合起来有两种做法,一种是先算出所有的双平方数,然后在这个集合中枚举a,b。另一种是直接枚举a,b,然后再逐一判断是否为双平方数,不过这种方法超时的风险很大,要很多剪枝才行。

鄙人果断方法一了。方法一如果不加入剪枝,5秒也是够呛的。而最简单有效的剪枝就是当a+b(n-1)>m时,根本不用再做下去了。

对了,这里我发现了原来freopen是定义在cstdio头文件中的,一直不知道T_T

#include <iostream>
#include <string>
#include <cstdio>
#include <string.h>
#include <algorithm>
using namespace std;bool f[2500005]={0};
int n,m,sum=0;
struct node{int aa,bb;friend bool operator < (node a,node b){return a.bb<b.bb;        //按照b的大小来排序
    }
};void init()
{cin>>n>>m;for (int p=0;p<=m;p++)for (int q=p;q<=m;q++)f[p*p+q*q]=true;         //直接用hash表,简单方便

}node rr[10005];
void work()
{int h=0,dd=0,k,sum=0,mm=2*m*m;for (int a=0;a<=mm;a++)         //a,b的次序可以交换,但是如果b在前a在后则剪枝不方便了if (f[a]){for (int b=1;b<mm;b++){if (a+b*(n-1)>mm) break;     //一个简单而强悍的剪枝,这里要是break,若是continue就根本没剪到了h=a; dd=b; k=0;while (f[h] && k<n){k++;h=h+dd;}if (k==n) {rr[++sum].aa=a;rr[sum].bb=b;}}}sort(rr+1,rr+sum+1);if (!sum) cout<<"NONE"<<endl;for (int i=1;i<=sum;i++)cout<<rr[i].aa<<" "<<rr[i].bb<<endl;return;
}int main()
{freopen("ariprog.in","r",stdin);freopen("ariprog.out","w",stdout);init();work();return 0;
}

转载于:https://www.cnblogs.com/ay27/archive/2012/10/10/2718735.html

USACO-Arithmetic Progressions相关推荐

  1. usaco Arithmetic Progressions

    题意:给出n,m,其中n表示等差数列的长度,每项由a^2+b^2表示,其中a和b不超过m,要求输出满足条件的首项及等差 思路:先根据m,可以得到由[0,m]构成的a^2+b^2,最大的等差,然后根据首 ...

  2. usaco Arithmetic Progressions(看了题解)

    usaco也开始限时了,这题是搜索加剪枝.剪枝很关键.(哎........怎么才能不看题解解题啊) /* ID: jinbo wu LANG: C++ TASK: ariprog */ #includ ...

  3. 2020牛客国庆集训派对day4 Arithmetic Progressions

    Arithmetic Progressions 链接:https://ac.nowcoder.com/acm/contest/7831/B 来源:牛客网 题目描述 An arithmetic prog ...

  4. Arithmetic Progressions

    一.问题 An arithmetic progression is a sequence of numbers a_1, a_2, . . . , a_ka1​,a2​,...,ak​ where t ...

  5. [USACO1.4]等差数列 Arithmetic Progressions

    题目 题目描述 一个等差数列是一个能表示成a, a+b, a+2b,..., a+nb (n=0,1,2,3,...)的数列. 在这个问题中a是一个非负的整数,b是正整数.写一个程序来找出在双平方数集 ...

  6. POJ3006-Dirichlet's Theorem on Arithmetic Progressions

    素数打表,水题. #include<stdio.h> #include<string.h> const int Max=1000001; bool isprime[Max]; ...

  7. 【CC November Challenge 2012】Arithmetic Progressions【分块】【FFT】

    题意:给定长度为 nnn 的正整数序列 AAA,求满足 i<j<k,Aj−Ai=Ak−Aji<j<k,A_j-A_i=A_k-A_ji<j<k,Aj​−Ai​=Ak ...

  8. USACO-Section1.5 Arithmetic Progressions(枚举)

    2017-6-7 题目描述 求出满足条件的等差数列的首项和公差 解答 枚举法 代码 /* ID: 18795871 PROG: ariprog LANG: C++ */ #include<ios ...

  9. 1.4 Arithmetic Progressions

    3个优化就可以过了...预处理所有bisquare并排序,以及一个bool数组判断是否有某个bisquare 我的思路是先b后a... 1.要满足有N个数  b <= MAXNUM/N 2.a ...

  10. Almost Arithmetic Progression

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

最新文章

  1. jquery选择器的使用方式
  2. 【APIO2014】Palindromes
  3. 吴恩达的TensorFlow实践课上线,有Python基础就能听,4个月学完
  4. 利用ajax技术 实现用户注册。
  5. @Repository详解
  6. 【任务悬赏】就地过年,原地充电,华为云社区喊你拿新年红包啦!
  7. virt-install选项详解
  8. 使用windbg通过vtable找到优化后的this指针
  9. 正确写理想的学术论文英文摘要
  10. android通知栏点击关闭,Android点击通知栏 ,移除通知
  11. 华为和荣耀学编程计算机推荐,2020~2021笔记本电脑推荐(华为篇)
  12. QVariant方法功能(QT5.12)
  13. NET开发邮件发送功能的全面教程(含邮件组件源码)(
  14. unicode转中文 中文转unicode的简单方式
  15. js 七大继承--史上最全最易懂
  16. 笔记本电脑同时连接内外网
  17. configure配置安装详解
  18. 软著中写源代码60页快速实现方法
  19. C语言:判断两数是否互质
  20. 内网如何设置代理上网?

热门文章

  1. Matlab-RBF网络(径向基函数网络)-rbepnngrnn
  2. 元宇宙游戏项目:Decentraland(治理通证:MANA)
  3. ncbi blast MATLAB,NCBI-BLAST在线使用教程详细攻略(图解)
  4. python3 打包后执行报错failed to execute script ***
  5. c语言资料大全收集,C语言库函数大全(收集资料).pdf
  6. uni-app注册全局组件
  7. Qt利用QGraphicsScene编写Word排版工具导出PDF
  8. MySQL自定义函数调用不出结果
  9. 以太坊君士坦丁堡:是利好?-千氪
  10. 18c和12客户端 oracle,客户端连接 12、18c 报ORA-28040和ORA-01017 的解决方法