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(推导、、)的更多相关文章

  1. hdoj 1977 Consecutive sum II

    Consecutive sum II Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  2. HDOJ(HDU).1003 Max Sum (DP)

    HDOJ(HDU).1003 Max Sum (DP) 点我挑战题目 算法学习-–动态规划初探 题意分析 给出一段数字序列,求出最大连续子段和.典型的动态规划问题. 用数组a表示存储的数字序列,sum ...

  3. HDOJ/HDU 1297 Children’s Queue(推导~大数)

    Problem Description There are many students in PHT School. One day, the headmaster whose name is Pig ...

  4. HDOJ(HDU) 2524 矩形A + B(推导公式、)

    Problem Description 给你一个高为n ,宽为m列的网格,计算出这个网格中有多少个矩形,下图为高为2,宽为4的网格. Input 第一行输入一个t, 表示有t组数据,然后每行输入n,m ...

  5. HDOJ(HDU).1258 Sum It Up (DFS)

    HDOJ(HDU).1258 Sum It Up (DFS) [从零开始DFS(6)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双 ...

  6. 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 ...

  7. HDOJ(HDU).1754 I Hate It (ST 单点替换 区间最大值)

    HDOJ(HDU).1754 I Hate It (ST 单点替换 区间最大值) 点我挑战题目 题意分析 从题目中可以看出是大数据的输入,和大量询问.基本操作有: 1.Q(i,j)代表求区间max(a ...

  8. HDOJ(HDU).1166 敌兵布阵 (ST 单点更新 区间求和)

    HDOJ(HDU).1166 敌兵布阵 (ST 单点更新 区间求和) 点我挑战题目 题意分析 根据数据范围和询问次数的规模,应该不难看出是个数据结构题目,题目比较裸.题中包括以下命令: 1.Add(i ...

  9. HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化)

    HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化) 题意分析 给出一系列的石头的数量,然后问石头能否被平分成为价值相等的2份.首先可以确定的是如果石头的价值总和为奇数的话,那 ...

随机推荐

  1. 黑白图像(DFS)

    输入一个n*n的黑白图像(1表示黑色,0表示白色),任务是统计其中八连块的个数.如果两个黑格子有公共边或者公共顶点,就说它们属于同一个八连块.如图6-11所示的图形有3个八连块. 图6-11  拥有3 ...

  2. java 数据库两种连接方法

    package jdbc; import java.sql.*; public class ConnectionDemo2 { public static final String DBDRIVER= ...

  3. php模拟HTTP协议发送post请求方法

    今天用到php模拟http发送post请求记录 代码如下: <?php $url = 'xxxx.com'; $data = 'a=one&b=two'; $data = urlenco ...

  4. 2015 UESTC Training for Search Algorithm & String - M - Palindromic String【Manacher回文串】

    O(n)的复杂度求回文串:Manacher算法 定义一个回文值,字符串S是K重回文串,当且仅当S是回文串,且其长度为⌊N/2⌋的前缀和长度为⌊N/2⌋的后缀是K−1重回文串 现在给一个2*10^6长度 ...

  5. 2015 UESTC Winter Training #10【Northeastern Europe 2009】

    2015 UESTC Winter Training #10 Northeastern Europe 2009 最近集训都不在状态啊,嘛,上午一直在练车,比赛时也是刚吃过午饭,状态不好也难免,下次比赛 ...

  6. group by、order by 先后顺序问题

    今天遇到个小问题 本来是很基础的问题 应该说 基础知道掌握的不牢  好了不说 错误 语句 :   select  a.a1  from table a  where order by a.a1 gro ...

  7. 关于php读mysql数据库时出现乱码的解决方法

    关于php读mysql数据库时出现乱码的解决方法 php读mysql时,有以下几个地方涉及到了字符集. 1.建立数据库表时指定数据库表的字符集.例如 create table tablename ( ...

  8. FckEditor组件的使用(新闻浏览发布页面)

    FckEditor组件的使用 准备工作: 1)将FckEditor目录拷贝到网站根目录内. 2)在Web.config的AppSetting分支中添加如下的代码,并修改FckEditor的目录和上传文 ...

  9. asp.net操作cookie类

    using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary ...

  10. Windows上帝模式

    在桌面新建一个文件夹 按F2重命名为 GodMode.{ED7BA470-8E54-465E-825C-99712043E01C} 完成