Arithmetic Progressions

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

分析:这题就是暴力搜索,剪枝,加了一个条件以后就过了。。没有再探寻更优解法。

代码如下:


#include <iostream>
#include <fstream>
#include <string.h>
#include <math.h>
#include <stdlib.h>
using namespace std;
struct PP
{
 int a;
 int b;
}ans[10001];
bool use[2*250*250+1]={0};
int find(int,int);
bool check(int,int,int);
int cmd(const void*,const void *);
int main()
{
 ofstream cout("ariprog.out");
 ifstream cin("ariprog.in");
 int n,m,count;
 cin>>n>>m;
 count=find(n,m);
 if(count==0) cout<<"NONE"<<endl;
 else
 {
 qsort(ans,count,sizeof(PP),cmd);
 for(int i=0;i<count;i++)
  cout<<ans[i].a<<" "<<ans[i].b<<endl;
 }
 return 0;
}

int find(int n,int m)
{
 int count=0;
 int p,q;
 for(p=0;p<=m;p++)
  for(q=0;q<=m;q++)
    use[q*q+p*p]=true;
 int max=2*m*m;
 for(int i=0;i<=max;i++)
  {
   for(int j=1;j<=max;j++)
   {
    if(i+(n-1)*j>max)
     break;
    if(check(i,j,n))
    {
     ans[count].a=i;
     ans[count].b=j;
     count++;
    }
   }
 }
 return count;
}
bool check(int a,int b,int n)
{
 if(use[a]==1)
 {
  int i;
  for(i=1;i<n;i++)
   if(use[a+i*b]==0)
    return 0;
  return 1;
 }
 else
  return 0;
}
int pow(int a,int b)
{
 int i,j;
 j=a;
 if(b==0) return 1;
 for(i=2;i<=b;i++)
  a*=j;
 return a;
}
int cmd(const void* a,const void* b)
{
 struct PP*c=(PP *)a;
 struct PP*d=(PP *)b;
 if(c->b!=d->b)
  return c->b>d->b?1:-1;
 else
  return c->a>d->a?1:-1;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值