正数负数分段求和

Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Description

Given two integers: n and m and n is divisible by 2m, you have to write down the first n natural numbers in the following form. At first take first mintegers and make their sign negative, then take next m integers and make their sign positive, the next m integers should have negative signs and continue this procedure until all the n integers have been assigned a sign. For example, let n be 12 and m be 3. Then we have

-1 -2 -3 +4 +5 +6 -7 -8 -9 +10 +11 +12

If n = 4 and m = 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 integer T (≤ 10000), denoting the number of test cases.

Each case starts with a line containing two integers: n and m (2 ≤ n ≤ 109, 1 ≤ m). And you can assume that n is divisible by 2*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

解题思路:
这道题刚开始想复杂了,n能被2m整除说明它可以分成n/2m个正负段。每一个正负段的和为m*m;所以输出m*n/2就行了!
注意要用long long型。
代码:
#include<stdio.h>
int main()
{
int t,k=1;
long long n,m;
scanf("%d",&t);
while(t--)
{
  scanf("%lld%lld",&n,&m);
  printf("Case %d: %lld\n",k++,n*m/2);
}
return 0;
 } 
在 Python 的 Pandas 库中,可以通过条件筛选分别对正数负数进行求和操作。以下是对 DataFrame 中正数负数分别求和的实现方法: ### 正数求和 可以通过条件筛选 `df > 0` 提取所有正数,然后使用 `sum()` 函数对这些值进行求和。此操作会忽略非数值类型的列[^1]。 ```python positive_sum = df[df > 0].sum() ``` ### 负数求和 同样地,可以通过条件筛选 `df < 0` 提取所有负数,然后使用 `sum()` 函数对这些值进行求和。此操作也仅适用于数值类型的列[^1]。 ```python negative_sum = df[df < 0].sum() ``` ### 对特定列的正数负数求和 如果仅需要对特定列进行正数负数求和,可以先选择该列,再应用条件筛选和求和操作[^1]。 ```python positive_sum_specific_column = df['column_name'][df['column_name'] > 0].sum() negative_sum_specific_column = df['column_name'][df['column_name'] < 0].sum() ``` ### 处理非数值列 如果 DataFrame 包含非数值列(例如字符串或日期时间类型),则需要先筛选出数值类型的列[^2]。 ```python numeric_df = df._get_numeric_data() positive_sum_numeric = numeric_df[numeric_df > 0].sum() negative_sum_numeric = numeric_df[numeric_df < 0].sum() ``` 上述代码通过 `._get_numeric_data()` 方法提取所有数值类型的列,从而避免非数值列导致的错误。 ### 示例代码 以下是一个完整的示例,包括创建 DataFrame、提取数值列以及对正数负数分别求和的操作。 ```python import pandas as pd import numpy as np # 创建包含正数负数和其他类型列的DataFrame df = pd.DataFrame({ 'a': [0, -1, 2], 'b': [-3, 2, 1], 'c': ['foo', 'goo', 'bar'], 'd': [np.datetime64('2023-01-01'), np.datetime64('2023-01-02'), np.datetime64('2023-01-03')] }) # 提取数值列 numeric_df = df._get_numeric_data() # 对正数负数分别求和 positive_sum = numeric_df[numeric_df > 0].sum() negative_sum = numeric_df[numeric_df < 0].sum() print("正数求和:") print(positive_sum) print("负数求和:") print(negative_sum) ``` ### 输出示例 假设 DataFrame 包含以下数据: ```python a b c d 0 0 -3 foo 2023-01-01 1 -1 2 goo 2023-01-02 2 2 1 bar 2023-01-03 ``` 输出结果为: ``` 正数求和: a 2 b 3 dtype: int64 负数求和: a -1 b -3 dtype: int64 ``` 此方法适用于对 DataFrame 中的数值列分别计算正数负数的总和,并能有效处理包含非数值列的数据。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值