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 中高效使用锁的技巧--转载
竞争锁是造成多线程应用程序性能瓶颈的主要原因 区分竞争锁和非竞争锁对性能的影响非常重要.如果一个锁自始至终只被一个线程使用,那么 JVM 有能力优化它带来的绝大部分损耗.如果一个锁被多个线程使用过,但 ...
- dedecms调用所有顶级栏目最新文章的实现方法
做dedecms的模板,我们会遇到各种各样的调用问题,dedecms列表页调用所有顶级栏目文章列表的方法如下所述: 在文章页面经常使用的是 {dede:arclist orderby='pubdate ...
- Datables wrning(table id='example'):Cannot reinitialise DataTable.
出现的问题如下所示: Datables wrning(table id='example' Datables object for this table,please pass eithser no ...
- 利用Range改变光标位置
先上代码,代码取自网上某插件中 function caret(begin, end) { if (this.length == 0) return; if (typeof begin == 'numb ...
- (转帖) 有限狀態機FSM coding style整理 (SOC) (Verilog)
来源:http://www.codesoso.net/Record/101092_95120_21.html 来源:http://www.cnblogs.com/oomusou/archive/201 ...
- 使用<br>标签分行显示文本
对于上一小节的例子,我们想让那首诗显示得更美观些,如显示下面效果: 怎么可以让每一句诗词后面加入一个折行呢?那就可以用到<br />标签了,在需要加回车换行的地方加入<br /> ...
- 你好,C++(11)如何用string数据类型表示一串文字?根据初始值自动推断数据类型的auto关键字(C++ 11)
3.5.2 字符串类型 使用char类型的变量我们可以表示单个字符,那么,我们又该如何表示拥有多个字符的字符串呢? 我们注意到,一个字符串是由多个字符串连起来形成的.很自然地,一种最简单直接的方法就 ...
- C语言零移位操作
给定一个整形数组要求把其中的零元素移动到数组的末尾 非零元顺序保持不变 以下采用两种方法实现 #include <stdlib.h> #include <stdio.h> #i ...
- JavaScript作用域链详解
JavaScript的作用域链还是很有味道的,搞懂了这个知识点,闭包的问题也就迎刃而解咯 1.JavaScript的全局变量和局部变量 首先,先来看看js的全局变量和局部变量,js不是块级作用域,所以 ...
- CSU 1337(费马大定理)
CSU 1337 Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%lld & %llu Descrip ...