CodeForces - 598A Tricky Sum (数学,快速幂的运用)
传送门:
http://codeforces.com/problemset/problem/598/A
1 second
256 megabytes
standard input
standard output
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.
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).
Print the requested sum for each of t integers n given in the input.
2
4
1000000000
-4
499999998352516354
The answer for the first sample is explained in the statement.
分析:
题意:
先输入一个T,表示有几组输入,再输入一个n,表示要计算n的和sum。
计算规则为,从1---n这n个数中任意一个数m,如果m这个数是2的次方的话,sum+=(-m),否则sum+=m。
思路:
先求出总的和sum,再用快速幂,求出所有是2的次方的数的和num,最后求差即可sum=sum-2*num。
code:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL qm(int n,int m)//快速幂
{
LL s=,x=n;
while(m)
{
if(m&)
{
s*=x;
}
x*=x;
m>>=;
}
return s;
}
int main()
{
int t;
cin>>t;
LL n;
LL sum;
while(t--)
{
scanf("%I64d",&n);
sum=n*(n+)/;//等差数列求和公式
for(int i=;qm(,i)<=n;i++)
{
sum-=(*qm(,i));
}
printf("%I64d\n",sum);
}
return ;
}
CodeForces - 598A Tricky Sum (数学,快速幂的运用)的更多相关文章
- codeforces 598A Tricky Sum
题目链接:http://codeforces.com/contest/598/problem/A 题目分类:大数 题意:1到n 如果是2的次方则减去这个数,否则就加上这个数,求最后的结果是多少 题目分 ...
- codeforces magic five --快速幂模
题目链接:http://codeforces.com/contest/327/problem/C 首先先算出一个周期里面的值,保存在ans里面,就是平常的快速幂模m做法. 然后要计算一个公式,比如有k ...
- 【codeforces 623E】dp+FFT+快速幂
题目大意:用$[1,2^k-1]$之间的证书构造一个长度为$n$的序列$a_i$,令$b_i=a_1\ or\ a_2\ or\ ...\ or a_i$,问使得b序列严格递增的方案数,答案对$10^ ...
- CodeForces - 691E Xor-sequences 【矩阵快速幂】
题目链接 http://codeforces.com/problemset/problem/691/E 题意 给出一个长度为n的序列,从其中选择k个数 组成长度为k的序列,因为(k 有可能 > ...
- Codeforces 954 dijsktra 离散化矩阵快速幂DP 前缀和二分check
A B C D 给你一个联通图 给定S,T 要求你加一条边使得ST的最短距离不会减少 问你有多少种方法 因为N<=1000 所以N^2枚举边数 迪杰斯特拉两次 求出Sdis 和 Tdis 如果d ...
- Codeforces 691E题解 DP+矩阵快速幂
题面 传送门:http://codeforces.com/problemset/problem/691/E E. Xor-sequences time limit per test3 seconds ...
- HDU 4599 Dice (概率DP+数学+快速幂)
题意:给定三个表达式,问你求出最小的m1,m2,满足G(m1) >= F(n), G(m2) >= G(n). 析:这个题是一个概率DP,但是并没有那么简单,运算过程很麻烦. 先分析F(n ...
- Codeforces 691E Xor-sequences(矩阵快速幂)
You are given n integers a1, a2, ..., an. A sequence of integers x1, x2, ..., xk is called a & ...
- codeforces 696C C. PLEASE(概率+快速幂)
题目链接: C. PLEASE time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
随机推荐
- nyoj 10——skiing————————【记忆化搜索】
skiing 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当 ...
- Linux内存管理机制简析
Linux内存管理机制简析 本文对Linux内存管理机制做一个简单的分析,试图让你快速理解Linux一些内存管理的概念并有效的利用一些管理方法. NUMA Linux 2.6开始支持NUMA( Non ...
- 转帖:kindeditor编辑区空格被隐藏,导致所见所得不一致的解决办法
1.修改kindereditor-all.js中的 var re = /(\s)<(/)?([\w-:]+)((?:\s+|(?:\s+[\w-:]+)|(?:\s+[\w-:]+=[^\s&q ...
- 【SoapUI】http接口测试
一.接口介绍 API(Application Programming Interface,应用程序编程接口) 1.硬件接口 USB接口 硬盘接口 SD卡接口 LAN口和WAN口 CONSOLE口 .. ...
- WebAssembly简单指导---译
开发者指导 本页面提供一步一步的操作将一个简单的程序编译成webassembly 前提要求 为了编译成webAssembly,需要提前安装一些工具: Git.在Linux和OSX下已自带了Git,在W ...
- 深入理解JavaScript系列(2):揭秘命名函数表达式
前言 网上还没用发现有人对命名函数表达式进去重复深入的讨论,正因为如此,网上出现了各种各样的误解,本文将从原理和实践两个方面来探讨JavaScript关于命名函数表达式的优缺点. 简单的说,命名函数表 ...
- .net生成cookie,读取cookie,创建特性
1.登录或注册成功时生成票据和cookie 注释:__JZY_Common_User_Login_Cookie__:就是为了判断当前登录人(例如:管理员和普通用户,可以定义两个常量,可有可无) 2.读 ...
- ECMAScript 6简介
一.起步 1.扎实的HTML/CSS/Javascript基本功,这是前置条件. 2.不要用任何的构建项目工具,只用最简单的<script>,把教程里的例子模仿一遍,理解用法.不推荐上来就 ...
- 003Angular2中使用ng-bootstrap
1.检查@angular/cli版本 命令行ng -v ,版本号必须大于1.0.0-beta.24 2.新建工程 工程所在目录,命令行ng new my-app --style=scss 带style ...
- 【起航计划 031】2015 起航计划 Android APIDemo的魔鬼步伐 30 App->Preferences->Advanced preferences 自定义preference OnPreferenceChangeListener
前篇文章Android ApiDemo示例解析(31):App->Preferences->Launching preferences 中用到了Advanced preferences 中 ...