Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
45 lines (43 loc) · 1.14 KB

File metadata and controls

45 lines (43 loc) · 1.14 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package tencent;
import java.util.*;
/**
* 翻转数列
*
* 小Q定义了一种数列称为翻转数列:
* 给定整数n和m, 满足n能被2m整除。对于一串连续递增整数数列1, 2, 3, 4..., 每隔m个符号翻转一次, 最初符号为'-';。
* 例如n = 8, m = 2, 数列就是: -1, -2, +3, +4, -5, -6, +7, +8.
* 而n = 4, m = 1, 数列就是: -1, +2, -3, + 4.
* 小Q现在希望你能帮他算算前n项和为多少。
* 输入描述:
* 输入包括两个整数n和m(2 <= n <= 109, 1 <= m), 并且满足n能被2m整除。
*
*
* 输出描述:
* 输出一个整数, 表示前n项和。
*
* 输入例子1:
* 8 2
*
* 输出例子1:
* 8
*/
public class Main2{
public static void main(String[] args){
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
int m=scan.nextInt();
long t=0;
//全部为负数时计算和
for(int i=1;i<=n;i++){
t+=-i;
}
long a=0;
//所有正数和
for(int i=1+m;i<=n;i+=2*m){
for(int j=i;j<i+m;j++){
a+=j;
}
}
System.out.println(""+(t+a*2));
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.