[LightOJ] [1294] Positive Negative Sign

Problem Description

Loading...

Problem Analysis

Input n m
Then there is an expression
Sum from 1 to n, with the sign changing every m numbers

For n=12, m=3:
-1 -2 -3 +4 +5 +6 -7 -8 -9 +10 +11 +12
For n=4, m=1:
-1 +2 -3 +4

What needs to be output is
the value of this expression

Computing it directly might be a bit troublesome
but we can look for a pattern
and find that the value of the expression
is exactly equal to $n*m/2$
This can be derived by manually working out several more cases

So the code becomes very simple~

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#include<stdio.h>
#include<string.h>
int main() {
	int T;
	int i;
	long long n,m,sum;
	scanf("%d",&T);
	for(i=1; i<=T; i++) {
		scanf("%lld %lld",&n,&m);
		printf("Case %d: %lld\n",i,n*m/2);
	}
	return 0;
}

Problem link: 【Light】[1294]Positive Negative Sign

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...