这道题目还是理解了很久的,英语烂真是头大。
题意是,求长度为N,且每个元素都是双平方数(双平方数是指一个数能被写成其他两个数的平方相加)的等比数列的个数。
给出长度N,和平方数的范围(两数q,p各小于M;也就是双平方数小于2*M*M)
暴搜+简单剪枝
/* ID: modengd1 PROG: ariprog LANG: C++ */ #include <iostream> #include <stdio.h> #include <queue> #include <memory.h> using namespace std; bool isBis[2*251*251]; int N,M; struct node { int a,b; node(int aa,int bb) { a=aa; b=bb; } node(){} bool friend operator<(node n1,node n2) { if(n1.b==n2.b) return n1.a>n2.a; else return n1.b>n2.b; } }; void Init() { memset(isBis,false,sizeof(isBis)); for(int i=0;i<=M;i++) { for(int j=0;j<=M;j++) { isBis[i*i+j*j]=true; } } } bool islegal(int a,int b) { if(!isBis[a+b*(N-1)]) return false; for(int i=0;i<N;i++) { if(!isBis[a]) return false; a+=b; } return true; } int main() { freopen("ariprog.in","r",stdin); freopen("ariprog.out","w",stdout); int ans=0; priority_queue<node> Q; scanf("%d",&N); scanf("%d",&M); Init(); for(int i=0;i<=M*M;i++) { for(int j=1;j<=M*M;j++) { if((i+(N-1)*j)>2*M*M)//简单剪枝 break; if(islegal(i,j)) { ans++; Q.push(node(i,j)); } } } if(Q.empty()) cout<<"NONE"<<endl; else while(!Q.empty()) { node now=Q.top(); Q.pop(); cout<<now.a<<‘ ‘<<now.b<<endl; } return 0; }
时间: 2024-08-01 12:26:02