Educational Codeforces Round 1 A. Tricky Sum 暴力
A. Tricky Sum
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/598/problem/A
Description
In this problem you are to calculate the sum of all integers from 1 to n, but you should take all powers of two with minus in the sum.
For example, for n = 4 the sum is equal to - 1 - 2 + 3 - 4 = - 4, because 1, 2 and 4 are 20, 21 and 22 respectively.
Calculate the answer for t values of n.
Input
The first line of the input contains a single integer t (1 ≤ t ≤ 100) — the number of values of n to be processed.
Each of next t lines contains a single integer n (1 ≤ n ≤ 109).
Output
Print the requested sum for each of t integers n given in the input.
Sample Input
2
4
1000000000
Sample Output
-4
499999998352516354
HINT
题意
让你输出前n个数的和,但是如果遇到了2的次方,就是减去他
题解:
正常做法,就直接O(1)得到前n个数的和,然后再log判断前n个数的2的倍数的数是哪些就好了。
但是我为什么脑抽了去写二分。。。
代码
#include<iostream>
#include<vector>
#include<stdio.h>
#include<algorithm>
using namespace std; vector<long long>Q;
vector<long long>Sum;
int main()
{
long long sum = ;
while(sum < 1e9+)
{
Q.push_back(sum);
sum*=;
}
long long Sum1 = ;
for(int i=;i<Q.size();i++)
{
Sum1 += Q[i];
Sum.push_back(Sum1);
}
int t;scanf("%d",&t);
while(t--)
{
long long n;cin>>n;
long long ans = (+n)*n/;
long long p = upper_bound(Q.begin(),Q.end(),n)-Q.begin();
p--;
ans -= *Sum[p];
cout<<ans<<endl;
}
}
Educational Codeforces Round 1 A. Tricky Sum 暴力的更多相关文章
- Educational Codeforces Round 53 E. Segment Sum(数位DP)
Educational Codeforces Round 53 E. Segment Sum 题意: 问[L,R]区间内有多少个数满足:其由不超过k种数字构成. 思路: 数位DP裸题,也比较好想.由于 ...
- Educational Codeforces Round 7 F. The Sum of the k-th Powers 拉格朗日插值法
F. The Sum of the k-th Powers 题目连接: http://www.codeforces.com/contest/622/problem/F Description Ther ...
- Educational Codeforces Round 8 A. Tennis Tournament 暴力
A. Tennis Tournament 题目连接: http://www.codeforces.com/contest/628/problem/A Description A tennis tour ...
- 【Educational Codeforces Round 37】F. SUM and REPLACE 线段树+线性筛
题意 给定序列$a_n$,每次将$[L,R]$区间内的数$a_i$替换为$d(a_i)$,或者询问区间和 这题和区间开方有相同的操作 对于$a_i \in (1,10^6)$,$10$次$d(a_i) ...
- Educational Codeforces Round 7 F - The Sum of the k-th Powers 拉格朗日插值
The Sum of the k-th Powers There are well-known formulas: , , . Also mathematicians found similar fo ...
- [Educational Codeforces Round 7]F. The Sum of the k-th Powers
FallDream dalao找的插值练习题 题目大意:给定n,k,求Σi^k (i=1~n),对1e9+7取模.(n<=10^9,k<=10^6) 思路:令f(n)=Σi^k (i=1~ ...
- Educational Codeforces Round 8 B. New Skateboard 暴力
B. New Skateboard 题目连接: http://www.codeforces.com/contest/628/problem/A Description Max wants to buy ...
- Educational Codeforces Round 19 E. Array Queries(暴力)(DP)
传送门 题意 给出n个数,q个询问,每个询问有两个数p,k,询问p+k+a[p]操作几次后超过n 分析 分块处理,在k<sqrt(n)时,用dp,大于sqrt(n)用暴力 trick 代码 #i ...
- Codeforces Educational Codeforces Round 17 Problem.A kth-divisor (暴力+stl)
You are given two integers n and k. Find k-th smallest divisor of n, or report that it doesn't exist ...
随机推荐
- 一天一个Java基础——数组
一天一个变成了几天一个,最近接受的新东西太多.太快,有好多需要blog的但没有时间,这些基础知识应该是要深挖并好好研究的,不应该每次都草草了事,只看个皮毛. 数组: JVM将数组存储在一个称为堆(he ...
- 【字符串处理】HDOJ-1020-Encoding
[题目链接:HDOJ-1020] 相邻字符,两两比较. #include<cstdio> #include<cstring> ; char sr[MAXN]; int main ...
- strleng函数和filterHtml函数的据悉
紧接着“CKEditor如何统计文字数量”,我想稍微说下,下面就filterHtml就是用来过滤全部html标签的. /** *过滤html标签 */ function filterHtml(s){ ...
- WebForm+Web.config: 超时时间已到。在操作完成之前超时时间已过或服务器未响应。
ylbtech-Error-WebForm+Web.config: 超时时间已到.在操作完成之前超时时间已过或服务器未响应. 超时时间已到.在操作完成之前超时时间已过或服务器未响应. 1.A,错误代码 ...
- MySQL安装之“测试”
将MySQL安装完成之后还需要对其进行测试,判断MySQL是否安装成功,MySQL其可视化与我们之前使用过的SQLserver不同.MySQL其中测试方法有两种:一.使用MySQL命令进行测试:二.安 ...
- 《Python 学习手册4th》 第十章 Python语句简介
''' 时间: 9月5日 - 9月30日 要求: 1. 书本内容总结归纳,整理在博客园笔记上传 2. 完成所有课后习题 注:“#” 后加的是备注内容 (每天看42页内容,可以保证月底看完此书) “重点 ...
- iOS多线程之GCD小记
iOS多线程之GCD小记 iOS多线程方案简介 从各种资料中了解到,iOS中目前有4套多线程的方案,分别是下列4中: 1.Pthreads 这是一套可以在很多操作系统上通用的多线程API,是基于C语言 ...
- Visual Assist X 破解步骤
1. 下载VA安装包,并点击exe文件安装(附下载地址:http://down.51cto.com/data/766817) 2. 将Visual Assist X Patch文件复制到C:\User ...
- MVC中CheckBox
一.单个Checkbox 1.View文件 <%= Html.CheckBoxFor(model => model.IsNeverExpired)%> 2.生成的HTML为 < ...
- [Hive - LanguageManual] DML: Load, Insert, Update, Delete
LanguageManual DML Hive Data Manipulation Language Hive Data Manipulation Language Loading files int ...