HDOJ(HDU) 1977 Consecutive sum II(推导、、)
Problem Description
Consecutive sum come again. Are you ready? Go ~~
1 = 0 + 1
2+3+4 = 1 + 8
5+6+7+8+9 = 8 + 27
…
You can see the consecutive sum can be representing like that. The nth line will have 2*n+1 consecutive numbers on the left, the first number on the right equal with the second number in last line, and the sum of left numbers equal with two number’s sum on the right.
Your task is that tell me the right numbers in the nth line.
Input
The first integer is T, and T lines will follow.
Each line will contain an integer N (0 <= N <= 2100000).
Output
For each case, output the right numbers in the Nth line.
All answer in the range of signed 64-bits integer.
Sample Input
3
0
1
2
Sample Output
0 1
1 8
8 27
看到有很多人的这个题目是找规律做的,我开始没注意规律了,
就打表做了。。。。
现在给出2种能AC的方法:
第一种:打表:
import java.util.Scanner;
public class Main{
static long[] db1 = new long[2100005];
public static void main(String[] args) {
dabiao();
//System.out.println("aa");
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
System.out.println(db1[n]+" "+db1[n+1]);
}
}
private static void dabiao() {
db1[0] = 0;
db1[1] = 1;
db1[2] = 8;
db1[3] = 27;
long k=9;
long num =7;
Scanner sc = new Scanner(System.in);
for(int i=4;i<=2100001;i++){
db1[i] =(k+num/2+1)*num-db1[i-1];
// System.out.println(k);
// System.out.println(num/2+1);
//
// System.out.println(i);
// System.out.println(db1[i]);
// System.out.println((k+num/2+1)*num);
// int m = sc.nextInt();
k=k+num;
num+=2;
}
}
}
第二种:找规律:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t =sc.nextInt();
while(t-->0){
long n =sc.nextLong();
System.out.println(n*n*n+" "+(n+1)*(n+1)*(n+1));
}
}
}
HDOJ(HDU) 1977 Consecutive sum II(推导、、)的更多相关文章
- hdoj 1977 Consecutive sum II
Consecutive sum II Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDOJ(HDU).1003 Max Sum (DP)
HDOJ(HDU).1003 Max Sum (DP) 点我挑战题目 算法学习-–动态规划初探 题意分析 给出一段数字序列,求出最大连续子段和.典型的动态规划问题. 用数组a表示存储的数字序列,sum ...
- HDOJ/HDU 1297 Children’s Queue(推导~大数)
Problem Description There are many students in PHT School. One day, the headmaster whose name is Pig ...
- HDOJ(HDU) 2524 矩形A + B(推导公式、)
Problem Description 给你一个高为n ,宽为m列的网格,计算出这个网格中有多少个矩形,下图为高为2,宽为4的网格. Input 第一行输入一个t, 表示有t组数据,然后每行输入n,m ...
- HDOJ(HDU).1258 Sum It Up (DFS)
HDOJ(HDU).1258 Sum It Up (DFS) [从零开始DFS(6)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双 ...
- HDU 1024 Max Sum Plus Plus (动态规划)
HDU 1024 Max Sum Plus Plus (动态规划) Description Now I think you have got an AC in Ignatius.L's "M ...
- HDOJ(HDU).1754 I Hate It (ST 单点替换 区间最大值)
HDOJ(HDU).1754 I Hate It (ST 单点替换 区间最大值) 点我挑战题目 题意分析 从题目中可以看出是大数据的输入,和大量询问.基本操作有: 1.Q(i,j)代表求区间max(a ...
- HDOJ(HDU).1166 敌兵布阵 (ST 单点更新 区间求和)
HDOJ(HDU).1166 敌兵布阵 (ST 单点更新 区间求和) 点我挑战题目 题意分析 根据数据范围和询问次数的规模,应该不难看出是个数据结构题目,题目比较裸.题中包括以下命令: 1.Add(i ...
- HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化)
HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化) 题意分析 给出一系列的石头的数量,然后问石头能否被平分成为价值相等的2份.首先可以确定的是如果石头的价值总和为奇数的话,那 ...
随机推荐
- Java基础知识强化之集合框架笔记09:Collection集合迭代器使用的问题探讨
1.Collection集合迭代器使用的问题探讨: (1)问题1:能用while循环写这个程序,我能不能用for循环呢? 可以使用for循环替代. (2)问题2:不要 ...
- css中var函数
引言: 在学习elementui的时候看到一个var.css, 其中写的全部都是以--开头的属性,上google查询不是css3新增的属性,于是决定一探究竟 :root { /* Transition ...
- Linux: FTP服务原理及vsfptd的安装、配置
1.FTP 服务的安装# yum install -y vsftpd [root@rusky pub]# ls -l /etc/vsftpd/ total 20 -rw-------. 1 root ...
- ubuntu 安装eclipse
安装步骤: 一.下载客户端: 解压放到,/opt/Java/eclipse目录下,解压方法参考上一篇文章<ubuntu 配置Java jdk> 二.打开eclipse: 打开终端,输入,c ...
- FileWriter类的flush方法的作用
FileWriter类的flush方法的作用 每次io都会影响性能,将需要写入的内容,放入缓冲区中,然后调用flush方法,将缓冲区内容写入文件中.
- 自定义UITableViewCell时, 使用autoLayout, 无法很好的做到屏幕适配
解决方法: 重写cell的setFrame方法即可 - (void)setFrame:(CGRect)frame { frame.size.width = self.window.frame.size ...
- Java中的继承与组合
本文主要说明Java中继承与组合的概念,以及它们之间的联系与区别.首先文章会给出一小段代码示例,用于展示到底什么是继承.然后演示如何通过“组合”来改进这种继承的设计机制.最后总结这两者的应用场景,即到 ...
- 初涉JavaScript模式 (2) : 基本技巧
尽量少用全局变量 大量使用全局变量会导致的后果 全局变量创建以后会在整个JavaScript应用和Web页面中共享.所有的全局变量都存在于一个全局命名空间内,很容易发生冲突 不知不觉创建了全局变量 其 ...
- 9 Palindrome Number(判断是否为回文数Easy)
题目意思:判断是否为回文数,不许使用额外空间 ps:一直不理解额外空间的意思,int能用吗 思路:1.比较头尾 2.翻转,越界问题需考虑 class Solution { public: bool i ...
- js循环便利json数据
var data=[{name:"a",age:12},{name:"b",age:11},{name:"c",age:13},{name: ...