zoj 2839 Find the Sequences(数学题)

来源:互联网 发布:阿里云服务器登录失败 编辑:程序博客网 时间:2024/05/16 12:15

转载请注明出处:http://blog.csdn.net/u012860063

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1839



题意:

要找所有满足条件的等差数列的首项和等差!

条件是等差数列的每一项(共n项),都能用小于等于m的两个数字的三次方的和(i*i*i+j*j*j == num);

要按等差从小到大输出,如果等差相同就按首项由小到大!

Find the Sequences

Time Limit: 2 Seconds      Memory Limit: 65536 KB

The sequence we discuss here is in the form a, a + b, a + 2b, a + 3b, ... where a is a non-negative integer and b is a positive integer. Your task is to find out all such sequences whose elements are of the form p3+q3 where p and q are non-negative integers.

Input:

The input consists of multiple test cases.

In each case, there're two integers n, m (3 <= n <= 10, 1 <= m <= 50). n, as mentioned above, is the number of terms in sequence, and m is the upper bound of p and q, which means 0 <= p, q <=m.

The input will be ended with "0 0" which should not be processed.

Output:

For each test case, output "Case #:" first. "#" is the number of the case, which starts from 1.

If no such sequence was found, simply output "NONE". Otherwise output the sequences in multiple lines, each line contains two integers a and b mentioned above. The sequence with smaller b comes first, if there's a tie, the one with smaller a comes first.

Separate two consecutive test cases with a blank line, but Do NOT output an extra blank line after the last one.

Sample Input:
4 109 10 0
Sample Output:
Case 1:152 6427 162Case 2:NONE

Author: DAI, Wenbin
Source: Zhejiang University Local Contest 2007

思路就是:先算出所有小于等于m的数字的所有三次的可能;

等差数列的每一下必然就在算出的这些数字中,这样大大可以减少时间;

代码如下:

#include <cstdio>#include <algorithm>#include <cstring>using namespace std;struct node{int a;int b;}no[5000];bool cmp(node e,node y){if(e.b==y.b)return e.a<y.a;return e.b<y.b;}int r[5000],f[300000];int main(){int n,m,p,q,i,j,k,KK,a1,a2,bb,x,count,flag,ans,u,pp,a3,yy;KK=1;while(~scanf("%d%d",&n,&m)&&(n+m)){if(KK!=1)printf("\n");printf("Case %d:\n",KK);KK++;k=0;memset(f,0,sizeof(f));for(i=0;i<=m;i++){for(j=0;j<=m;j++){pp=i*i*i+j*j*j;if(f[pp]==0){f[pp]=1;r[k++]=pp;}}}sort(r,r+k);flag=0;ans=0;u=0;for(i=0;i<k-n;i++){a1=r[i];for(j=i+1;j<k;j++){a2=r[j];bb=a2-a1;for(count=3;count<=n;count++){a3=a1+(count-1)*bb;if(a3>r[k-1]||f[a3]==0)break;}if(count>n){flag=1;no[u].a=a1;no[u++].b=bb;}}}sort(no,no+u,cmp);if(flag){for(i=0;i<u;i++){printf("%d %d\n",no[i].a,no[i].b);}}elseprintf("NONE\n");}return 0;}


0 0
原创粉丝点击