J - Positive Negative Sign

本文介绍了一种针对特定整数序列的高效求和算法。该算法通过将序列中的整数按规律分配正负号,然后计算总和。输入包括一系列测试用例,每个用例由两个整数n和m组成,输出则是按照指定规则计算得到的序列总和。

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 m integers 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 

12 3 
4 1 
Output for Sample Input 
Case 1: 18 

Case 2: 2

int n,m;          NND坑死我了

#include<stdio.h>
int main()
{
	long long n,m;
	int t,c=1;
	scanf("%d",&t);
	while(t--){
		long long sum;
		scanf("%lld%lld",&n,&m);
		sum=n/2*m;
		printf("Case %d: %lld\n",c++,sum);
	}
}


/*---------------------------------------------------------------------- | st_ref_pic_set +---------------------------------------------------------------------*/ static AP4_Result parse_st_ref_pic_set(AP4_HevcShortTermRefPicSet* rps, const AP4_HevcSequenceParameterSet* sps, unsigned int stRpsIdx, unsigned int num_short_term_ref_pic_sets, AP4_BitReader& bits) { AP4_SetMemory(rps, 0, sizeof(*rps)); unsigned int inter_ref_pic_set_prediction_flag = 0; if (stRpsIdx != 0) { inter_ref_pic_set_prediction_flag = bits.ReadBit(); } if (inter_ref_pic_set_prediction_flag) { unsigned int delta_idx_minus1 = 0; if (stRpsIdx == num_short_term_ref_pic_sets) { delta_idx_minus1 = ReadGolomb(bits); } /* delta_rps_sign = */ bits.ReadBit(); /* abs_delta_rps_minus1 = */ ReadGolomb(bits); if (delta_idx_minus1+1 > stRpsIdx) { DBG_PRINTF_2("delta_idx_minus1[%d]+1 > stRpsIdx[%d]\n", delta_idx_minus1, stRpsIdx); return AP4_ERROR_INVALID_FORMAT; // should not happen } unsigned int RefRpsIdx = stRpsIdx - (delta_idx_minus1 + 1); unsigned int NumDeltaPocs = sps->short_term_ref_pic_sets[RefRpsIdx].num_negative_pics + sps->short_term_ref_pic_sets[RefRpsIdx].num_positive_pics; for (unsigned j=0; j<=NumDeltaPocs; j++) { unsigned int used_by_curr_pic_flag /*[j]*/ = bits.ReadBit(); if (!used_by_curr_pic_flag /*[j]*/) { /* use_delta_flag[j] = */ bits.ReadBit(); } } } else { rps->num_negative_pics = ReadGolomb(bits); rps->num_positive_pics = ReadGolomb(bits); if (rps->num_negative_pics > 16 || rps->num_positive_pics > 16) { DBG_PRINTF_2("rps->num_negative_pics[%d] > 16 || rps->num_positive_pics[%d] > 16\n", rps->num_negative_pics, rps->num_positive_pics); return AP4_ERROR_INVALID_FORMAT; } for (unsigned int i=0; i<rps->num_negative_pics; i++) { rps->delta_poc_s0_minus1[i] = ReadGolomb(bits); rps->used_by_curr_pic_s0_flag[i] = bits.ReadBit(); } for (unsigned i=0; i<rps->num_positive_pics; i++) { rps->delta_poc_s1_minus1[i] = ReadGolomb(bits); rps->used_by_curr_pic_s1_flag[i] = bits.ReadBit(); } } return AP4_SUCCESS; } /*---------------------------------------------------------------------- | NumPicTotalCurr +---------------------------------------------------------------------*/ static unsigned int NumPicTotalCurr(const AP4_HevcShortTermRefPicSet* rps, const AP4_HevcSliceSegmentHeader* slice_segment_header) { // compute NumPicTotalCurr unsigned int nptc = 0; if (rps) { for (unsigned int i=0; i<rps->num_negative_pics; i++) { if (rps->used_by_curr_pic_s0_flag[i]) { ++nptc; } } for (unsigned int i=0; i<rps->num_positive_pics; i++) { if (rps->used_by_curr_pic_s1_flag[i]) { ++nptc; } } } for (unsigned int i=0; i<slice_segment_header->num_long_term_sps + slice_segment_header->num_long_term_pics; i++) { if (slice_segment_header->used_by_curr_pic_lt_flag[i]) { ++nptc; } } // TODO: for now we assume pps_curr_pic_ref_enabled is 0 //if (pps_curr_pic_ref_enabled) { // ++nptc; //} return nptc; } 注释上述代码并保留原有注释掉的内容
最新发布
09-24
--{{ Section below this comment is automatically maintained -- and may be overwritten --{entity {AdcCalibrationVhd} architecture {AdcCalibrationVhd}} library ieee; use ieee.std_logic_1164.all; use ieee.numeric_std.all; entity AdcCalibrationVhd is port( plv12AdcValueRaw : in std_logic_vector( 11 downto 0 ); plv16CalibrationValue : in std_logic_vector( 15 downto 0 ); plInvertAdcValue : in std_logic; plv16AdcValueCal : out std_logic_vector( 15 downto 0 ) := ( others => '0' ) ); end AdcCalibrationVhd; --}} End of automatically maintained section architecture AdcCalibrationVhd of AdcCalibrationVhd is signal ssv17AdcResized : signed( 16 downto 0 ) := ( others => '0' ); signal ssv17CalibrationResized : signed( 16 downto 0 ) := ( others => '0' ); signal ssv17Sum : signed( 16 downto 0 ) := ( others => '0' ); signal ssv17Final : signed( 16 downto 0 ) := ( others => '0' ); begin -- Following steps are handled in this module: -- 1) Resize raw 12 bit ADC data to 17 bits (final result is 16 bits, but one more bit is used to prevent overflow when adding calibration data) -- 2) Resize 16 bit calibration data to 17 bits -- 3) Add 17 bit ADC and calibration data -- 4) Invert requested channels (to compensate for rotated hardware current sensors) -- 5) Limit final value to 16 bits -- Step 1: Resize raw 12 bit ADC data to 17 bits (S = sign bits, P = padding bits) -- S S S PPPP -- Positive max = 2047 = 0_111_1111_1111 ===> 16 * 2047 = 32752 = 0_0_111_1111_1111_0000 -- Positive one = 1 = 0_000_0000_0001 ===> 16 * 1 = 16 = 0_0_000_0000_0001_0000 -- Zero = 0 = 0_000_0000_0000 ===> 16 * 0 = 0 = 0_0_000_0000_0000_0000 -- Negative one = -1 = 1_111_1111_1111 ===> 16 * -1 = -16 = 1_1_111_1111_1111_0000 -- Negative max = -2048 = 1_000_0000_0000 ===> 16 * -2048 = -32768 = 1_1_000_0000_0000_0000 ssv17AdcResized <= signed( plv12AdcValueRaw( 11 ) & plv12AdcValueRaw( 11 downto 0 ) & "0000" ); -- Step 2: Resize 16 bit calibration data to 17 bits ssv17CalibrationResized <= signed( plv16CalibrationValue( 15 ) & plv16CalibrationValue( 15 downto 0 )); -- Step 3: Calculate sum of ADC and calibration value (calibration value is small, so there is no risk of overflow) ssv17Sum <= ssv17AdcResized + ssv17CalibrationResized; -- Step 4: Invert requested channels Invert : process( plInvertAdcValue, ssv17Sum ) begin if plInvertAdcValue = '1' then ssv17Final <= -ssv17Sum; else ssv17Final <= ssv17Sum; end if; end process Invert; -- Step 5: Limit final value to 16 bits Limit : process( ssv17Final ) begin if ssv17Final > 32767 then plv16AdcValueCal <= std_logic_vector( to_signed( 32767, 16 )); elsif ssv17Final < -32768 then plv16AdcValueCal <= std_logic_vector( to_signed( -32768, 16 )); else plv16AdcValueCal <= std_logic_vector( ssv17Final( 16 ) & ssv17Final( 14 downto 0 )); end if; end process Limit; end AdcCalibrationVhd; 请解析这一段VHDL代码
08-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值