[CodeForces] [598A] Tricky Sum

Problem Description

Loading...

Problem Analysis

A pretty easy problem
First compute the sum from 1 to n using the formula
Then subtract 2 to the power of i one by one
Until 2 to the power of i is greater than n

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include<stdio.h>
int main() {
	int T;
	scanf("%d",&T);
	while(T--) {
		int n;
		scanf("%d",&n);
		__int64 res=(__int64)n*(__int64)(n+1)/2;
		int t=1;
		while(1) {
			res-=t*2;
			if(t>n/2)
				break;
			t*=2;
		}
		printf("%I64d\n",res);
	}
	return 0;
}

Problem URL:【CodeForces】[598A]Tricky Sum

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...