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基础——异常处理
异常的层次结构 所有的异常类都是 java.lang.Exception 类的子类型.异常类都是 Throwable 类的子类.除了异常类 Error 类也是由 Throwable 类产生的的子类1. ...
- TCP/IP详解学习笔记(6)-UDP协议
1.UDP简要介绍 UDP是传输层协议,和TCP协议处于一个分层中,但是与TCP协议不同,UDP协议并不提供超时重传,出错重传等功能,也就是说其是不可靠的协议. 2.UDP协议头 2.1.UDP端口号 ...
- 应用市场高速下载以及网页端调起APP页面研究与实现
Github博文地址,此处更新可能不是很及时. 好久没写博客了,好大一个坑.正好,最近刚做完应用市场的高速下载功能,便拿来填了这个坑. 话说产品为了增加用户量,提升用户活跃度以及配合推广,更坑爹的是看 ...
- Oracle EM 不能访问
zwt2001267 原文 Oracle EM 不能访问 1. cmd控制启动EM: C:\Users\Administrator>emctl start dbconsoleEnvironmen ...
- Android WindowManager 监听返回键及home键
一.监听home键盘,Android Home键系统负责监听,捕获后系统自动处理.有时候,我们需要监听home键处理自己的逻辑,监听方法如下: /** * 监听home键广播 */ private f ...
- Spring的事务传播属性,数据库的隔离级别
Spring事务的传播属性 REQUIRED 业务方法需要在一个事务中运行,如果方法运行时,已处在一个事务中,那么就加入该事务,否则自己创建一个新的事务.这是spring默认的传播行为. SUPPO ...
- UILabel 自适应大小
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStylealloc]init]; paragraphStyle.lineB ...
- bzoj 3171 [Tjoi2013]循环格(MCMF)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3171 [题意] 给定一个方向矩阵,要求改变最少的格子,使得任意一个点都在一个环中. [ ...
- Bias/variance tradeoff
线性回归中有欠拟合与过拟合,例如下图: 则会形成欠拟合, 则会形成过拟合. 尽管五次多项式会精确的预测训练集中的样本点,但在预测训练集中没有的数据,则不能很好的预测,也就是说有较大的泛化误差,上面的右 ...
- Todolist
UValive 6041(KD tree) UValive 6042(DP) UValive 6044(图论)