列表

详情


NC200113. Little Gyro and Sets

描述

Little Gyro has just found an empty integer set A in his right pocket, and an empty integer set B in his left pocket. At the same time, Little Gyro has already thought about two integers n, m in his mind. As Little Gyro is bored, he decides to play with these two sets.
Then, Little Gyro is ready to divide the integer series to these two empty sets. With the following rules:
1. If the number is an integer multiple of n, Little Gyro will put it in set A.
2. Otherwise, Little Gyro will put it in set B instead.
Now given two integers n, m, after these operations, Little Gyro wants to know the result of sum(B)-sum(A) and ask you for help, please help him to calculate.

输入描述

There are multiple test cases. The first line of the input contains an integer T (1 ≤ T ≤ ), indicating the number of test cases. For each test case:
Each line contains two integers n, m (1 ≤ n, m ≤ ), indicating the length of the sequence.

输出描述

For each test case output an integer indicating the result of sum(B)-sum(A).

示例1

输入:

3
3 5
2 10
3 16

输出:

9
-5
46

说明:

For the first sample, after these operations, A = < 3 >, B = < 1, 2, 4, 5 >, and the answer is 9.
For the second sample, after these operations, A = < 2, 4, 6, 8, 10 >, B = < 1, 3, 5, 7, 9 >, and the answer is -5.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Python3(3.5.2) 解法, 执行用时: 943ms, 内存消耗: 5096K, 提交时间: 2019-12-07 16:45:00

def solve(n, m):
    ans_B = ((m + 1) * m) // 2
    sum = m // n
    ans_A = (sum * (2*n+n*(sum-1))) // 2
    return ans_B - 2*ans_A


t = int(input())
while t > 0:
    t -= 1
    n, m = list(map(int, input().split(' ')))
    print(solve(n, m))

C++14(g++5.4) 解法, 执行用时: 185ms, 内存消耗: 2148K, 提交时间: 2019-12-07 15:06:30

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int a;
	scanf("%d",&a);
	while(a--)
	{
		long n,m,sum1=0,sum2=0;
		scanf("%ld%ld",&n,&m);
		sum1=(m+1)*m/2;
		sum2=(n+n*(m/n))*(m/n)/2;
		cout<<sum1-2*sum2<<endl;
	}
} 

C++11(clang++ 3.9) 解法, 执行用时: 53ms, 内存消耗: 2148K, 提交时间: 2020-02-25 13:44:12

#include<stdio.h>
int main()
{
	long long int n,a,b;
	scanf("%lld",&n);
	while(n--)
	{
		scanf("%lld%lld",&a,&b);
		printf("%lld\n",b*(b+1)/2-a*(1+b/a)*(b/a));
	}
	return 0;
}

上一题