J-positive negative sign
Time limit:2000 ms
Memory limit:32768kb
64bit
Io format:Unknown
Description
Given two integers:NAndMAndNIs divisible2 m, You have to write down the firstNNatural Numbers in the following form. At first take
FirstMIntegers and make their sign negative, then take nextMIntegers and make their sign positive, the nextMIntegers shoshould have negative signs and continue this procedure until allNIntegers
Have been assigned a sign. For example, letNBe12AndMBe3. Then we have
-1-2-3+ 4 + 5 + 6-7-8-9+ 10 + 11 + 12
IfN = 4AndM = 1, Then we have
-1+ 2-3+ 4
Now your task is to find the summation of the numbers considering their signs.
Input
Input starts with an integerT (≤ 10000), Denoting the number of test cases.
Each case starts with a line containing two integers:NAndM (2 ≤ n ≤ 109, 1 ≤ m). And you can assume thatNIs divisible2 * m.
Output
For each case, print the case number and the summation.
Sample Input
2
12 3
4 1
Sample output
Case 1: 18
Case 2: 2
Analysis:
Simple question. (It's always hard to find out that it's a simple question ...)
When you look at the data size, you know that it is time-out to start from left to right. (I tried it again, wasting so much time ...)
I only think of a few cases of such large-scale data: Binary? Dynamic Regulation? Or a mathematical formula?
Obviously, this is a mathematical formula. We can find that N can be divisible by 2 M: All numbers must be first reduced by M and then increased by M. Then we further find that, after each subtraction, M * m will be added to the final result. For example,-1,-2,-3, + 4, + 5, + 6 can be viewed as (4-1) + (5-2) + (6-3 ). In total, n/2 m times are added after the first subtraction. So the final result is M * m * n/2 M = N * m/2.
Here we also need to observe the range of numbers: N = 1000000000 M = 500000000 in the worst case, so the final answer needs long.
#include <iostream>using namespace std;int main(){ int t; long long n,m; cin>>t; for (int i=0;i<t;i++) { cin>>n>>m; long long ans=n*m/2; cout<<"Case "<<i+1<<": "<<ans<<endl; } return 0;}