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);
要按等差从小到大输出,如果等差相同就按首项由小到大!
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 0Sample 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;}
- zoj 2839 Find the Sequences(数学题)
- zju 2839 Find the Sequences
- hdu 1597 find the nth digit(数学题)
- HDU 1597 find the nth digit (数学题)
- ZOJ 3605 Find the Marble(DP)
- ZOJ 3605 Find the Marble(dp)
- zoj 3605 Find the Marble (dp)
- Find the Marble ZOJ
- hdu 1597 数学题 find the nth digit
- ZOJ 3605Find the Marble
- zoj 1530 Find The Multiple
- ZOJ 3605 Find the Marble
- ZOJ 3605Find the Marble
- zoj 3860 Find the Spy
- ZOJ 3860: - Find the Spy
- ZOJ 3605 Find the Marble
- ZOJ 3605 Find the Marble(dp啊 三维)
- ZOJ 3432 Find the Lost Sock(模拟)
- JAVA过滤html标签的方法
- 校赛B 长单词
- c函数之【格式化输人输出函数】
- cocos2dx-新建工程时避免文件和文件夹的拷贝
- 9.菜鸟启程,向大牛进发
- zoj 2839 Find the Sequences(数学题)
- c函数之【文件及目录函数】
- 高效程序员的 7 个共同特征
- C/C++实现矩阵相乘(一)
- DCMTK-Configure the logger from a program
- 简单使用jsoup
- 关于共享文件夹
- [2012校赛新生组]狡兔三窟
- 写在开博前的话