[USACO 1.4.3]等差数列

地址:http://hustoj.sinaapp.com/problem.php?id=1831

先枚举公差,再枚举首项,这样可以直接输出

在初始化中已经知道哪些数是双平方数,哪些不是

 1 #include <iostream>
 2 #include <algorithm>
 3 #define MAX 250
 4 using namespace std;
 5 
 6 int n,m;
 7 bool s[MAX*MAX*2+1];
 8 
 9 void ini()
10 {
11     int i,j;
12     for(i=0;i<=m;i++)
13         for(j=0;j<=m;j++)
14             s[i*i+j*j]=true;
15 }
16 
17 bool check(int a,int b)
18 {
19     for(int i=0;i<n;i++)
20         if(!s[a+i*b]) return false;
21     return true;
22 }
23 
24 int main()
25 {
26     int a,b,lim;
27     bool flag=true;
28     ios::sync_with_stdio(false);
29     cin>>n>>m;
30     ini();
31     lim=2*m*m;
32     for(b=1;b<=lim;b++)
33         for(a=0;a+(n-1)*b<=lim;a++)
34         {
35             if(check(a,b))
36             {
37                 cout<<a<<" "<<b<<endl;
38                 flag=false;
39             }
40         }
41     if(flag) cout<<"NONE\n";
42     return 0;
43 }

 

转载于:https://www.cnblogs.com/tjsuhst/archive/2013/01/24/2875420.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值