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 ...
随机推荐
- TOJ 2888 Pearls
Description In Pearlania everybody is fond of pearls. One company, called The Royal Pearl, produces ...
- Android中改变Activity的不同icon:activity-alias
Android设置title中的Icon有几种方法,介绍如下: 一种是直接在AndroidManifest.xml文件中设置android:icon属性,这种方法简单有效,应该算是我们最常用的设置Ic ...
- 虚拟机vmware 上不去 连不上网问题解决
开始---设置--控制面板---管理工具---服务 确保 VMware DHCP Service 和VMware NAT Service 服务已经启动
- pL/SQL 创建DBLIKN
某些时候,需要关联不同的数据库进行数据查询.操作等. 在Oracle中,关联不同的数据库进行表关联,就要用到了数据库连接(DB link). 创建DB link有两种方法:通过SQL语句创建,通过可视 ...
- APP测试点集合
一.功能性测试: (1)根据产品需求文档编写测试用例 (2)软件设计文档编写用例 二.兼容性适配性测试: (1)Android.iOS版本的兼容性 (2)手机分辨率兼容性 (3)网络的兼容性:2G/3 ...
- 自己实现C++的string类
使用C++的朋友对string类都不会陌生,string类使用起来很是方便,比C的字符数组好用多了,但是我们能不能自己实现一个属于自己的string类呢? 实现一个具有基本功能的string类并不难, ...
- jquery监控input输入框的变化
(function($) { $.fn.watch = function(callback) { return this.each(function() { //缓存以前的值 $.data(this, ...
- uiautomator 1使用简介
1.生成build.xml android create uitest-project -n jar_name -t id -p projectPah 2.修改build.xml 改成默认执行 bu ...
- 2017年9月17日 JavaScript简介
javascript简介 javascript是个什么东西? JavaScript是个脚本语言,需要有宿主文件,它的宿主文件就是html文件. 它与java有什么关系? 没有什么直接联系,java是s ...
- .net Ioc 之 Unity 适合刚开始使用
介绍: 首先稍微介绍一下,Unity是微软patterns& practices组用C#实现的轻量级.可扩展的依赖注入容器,可通过代码或xml配置文件来配置对象之间的关系.那么通过一个简单的代 ...