2的0-n方和

来源:互联网 发布:大唐移动面试知乎 编辑:程序博客网 时间:2024/04/30 06:23
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit Status

Description

Find the number of k-divisible numbers on the segment [a, b]. In other words you need to find the number of such integer values x thata ≤ x ≤ b and x is divisible by k.

Input

The only line contains three space-separated integers ka and b (1 ≤ k ≤ 1018; - 1018 ≤ a ≤ b ≤ 1018).

Output

Print the required number.

Sample Input

Input
1 1 10
Output
10
Input
2 -4 4
Output
5

解题思路:
这道题还是收获的,主要在计算从0到n之间2^0,2^1,2^2.......的和; 
int sum=0;
for(i=1;i<=n;i=i*2)
{
    sum=sum+i;



代码:
#include<stdio.h>
int main()
{
__int64 m,n,t,i;
scanf("%I64d",&t);
while(t--)
{    
   __int64  j=0;
   scanf("%I64d",&n);
   m=n*(n+1)/2; 
for(i=1;i<=n;i=i*2)
{
j=j-i;
}
printf("%I64d\n",m+2*j);

return 0;
 } 
0 0