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 ...
随机推荐
- bootstrap框架的使用
1.默认修改input输入框激活的颜色(充电桩) .form-control:focus, .ms-choice:focus, input[type=text]:focus, input[type=p ...
- console命令详解:(转载学习)
Console命令详解,让调试js代码变得更简单 Firebug是网页开发的利器,能够极大地提升工作效率. 但是,它不太容易上手.我曾经翻译过一篇<Firebug入门指南>,介绍了一些 ...
- ionic 打包安卓包 (debug调试版和 release发布版)
一.配置环境: 先按照之前的文章,配置好环境需要: 安装jdk,配置环境变量:( http://www.cnblogs.com/loveyaxin/p/7520618.html) 安装android- ...
- ContextCapture水面约束(水面破洞修复)
[问题描述] 对于水面而言,由于特征点较少,软件在计算时很难匹配正确,导致输出模型的水面通常是支离破碎的.软件针对这种情况提供了一个约束工具,用户手动的为水面添加平面约束后,输出的水面模型就会非常 ...
- opencv2.4.10+VS2012配置问题
opencv2.4.10+VS2012配置 作为opencv的初学者,第一个难题想必都一样,如何配置opencv+VS的环境呢?在网上的教程,铺天盖地,但我仍然是尝试了十几次才找到属于自己的那套配置方 ...
- 标准c库函数和linux系统函数的关系
c库IO函数的工作流程 c库函数与系统函数的关系 虚拟地址空间 文件描述符
- json中定义数组
我们经常会看到在js中定义普通函数: function f1(a,b){ console.log(a+b); } f1(); 今天我们看一下如何在json里边定义函数并调用: var json = { ...
- 30 Excellent WordPress Video Tutorials
http://sixrevisions.com/wordpress/30-excellent-wordpress-video-tutorials/ WordPress是一种使用PHP语言开发的博客平台 ...
- 前端小课堂 js:what is the function?
js 函数: 概念:函数是由事件驱动的或者当它被调用时执行的可重复使用的代码块. 说白了就是响应用户操作所执行的代码,通过js事件触发,然后调用执行函数里代码的操作. 比如常见的用户点击事件,用户点击 ...
- JSP初学者3
reponse代表服务器对客户端的响应.大部分时候,程序无须使用response来响应客户端请求,因为有更简单的响应对象——out,它代表页面输出流. 但out无法响应生成非字符内容(out是JspW ...