NC200113. Little Gyro and Sets
描述
输入描述
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.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; }