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 ...
随机推荐
- Eclipse error: “The import XXX cannot be resolved”
解决 Eclipse error: “The import XXX cannot be resolved” eclipse中修改: 1. 项目-->Properties-->java bu ...
- 如何优雅地进行错误处理(clean code阅读笔记之六)
错误处理是十分必要的,但是如果对错误处理使用不当则会让代码变得十分臃肿,让阅读者看不清代码的逻辑,更严重的是,这也会让程序变得十分脆弱.本文中将列出一些使用错误处理的技巧,帮助你写出既简洁又健壮的代码 ...
- ArrayList 集合
ArrayList 集合:很多数据的一个集合 数组:长度不可变.类型单一 集合的好处:长度可以任意改变 类型随便 集合长度都的问题 很多数据的集合数组类型不可变 长度单一 ...
- linux安装git、node、pm2
一.安装Git 下载:# wget https://www.kernel.org/pub/software/scm/git/git-2.9.5.tar.gz 解压:# tar -zxvf git-2. ...
- 初识Socket通信:基于TCP和UDP协议学习网络编程
学习笔记: 1.基于TCP协议的Socket网络编程: (1)Socket类构造方法:在客户端和服务器端建立连接 Socket s = new Socket(hostName,port);以主机名和端 ...
- [LeetCode]25. Reverse Nodes in k-Group k个一组翻转链表
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k ...
- 关于Java中用Double型运算时精度丢失的问题
注:转自 https://blog.csdn.net/bleach_kids/article/details/49129943 在使用Java,double 进行运算时,经常出现精度丢失的问题,总是在 ...
- 编写可维护的 Gruntfile.js
load-grunt-tasks 插件 首先介绍下 load-grunt-tasks 这个插件. 我们一般都会把所有用到的插件以及插件的配置写到 Gruntfile.js 里面,对于小项目来说这个文件 ...
- css一个很好用的hover显示
之前一直想在样式实现,hover时显示其他div,今天终于找到了,(*^▽^*) .a悬停时显示.b .a:hover .b { display: block; }
- win10下clodeblocks编译C语言乱码
打开settings->compile,在other compiler options添加下面两行代码: -fexec-charset=GBK-finput-charset=UTF-8