zoj 2839 Find the Sequences(数学题)
2014-03-29 22:02
302 查看
转载请注明出处: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:
Sample Output:
Author: DAI, Wenbin
Source: Zhejiang University Local Contest 2007
思路就是:先算出所有小于等于m的数字的所有三次的可能;
等差数列的每一下必然就在算出的这些数字中,这样大大可以减少时间;
代码如下:
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1839
----------------------------------------------------------------------------------------------------------------------------------------------------------
欢迎光临天资小屋 :http://user.qzone.qq.com/593830943/main
----------------------------------------------------------------------------------------------------------------------------------------------------------
题意:
要找所有满足条件的等差数列的首项和等差!
条件是等差数列的每一项(共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 10 9 1 0 0
Sample Output:
Case 1: 152 64 27 162 Case 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); } } else printf("NONE\n"); } return 0; }
相关文章推荐
- 如何解决ArrayAdapter requires the resource ID to be a TextView
- 怎样用java生成GUID与UUID
- HDUOJ ---1423 Greatest Common Increasing Subsequence(LCS)
- 用ServletContext获得Web.xml中子元素param-value的值异常
- pcduino上配置samba服务器
- 关于UIScrollViewDelegate协议中每个回调函数的意义及执行
- 关于CEGUI环境搭建的图文教程(主要致力于OGRE1.9.0)
- UE4 AI浅析-shooterGame中的AI为例
- UE4学习笔记_07
- 手势操作---识别单击还是双击
- unigui数据库连接池
- ueditor编辑器图片自定义存放目录及路径修改
- Mavericks系统安装的一些小细节
- UEditor上传图片错误
- Number Sequence
- Number Sequence
- C#中用GUID做种子改善随机效果
- UE4学习笔记_06
- UE4中的AI解析1
- ios uicolor的分类——十六进制的颜色转换为UIColor