hdu 5587 Array

简介:

Array

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 255    Accepted Submission(s): 133


Problem Description
Vicky is a magician who loves math. She has great power in copying and creating.
One day she gets an array {1}。 After that, every day she copies all the numbers in the arrays she has, and puts them into the tail of the array, with a signle '0' to separat.
Vicky wants to make difference. So every number which is made today (include the 0) will be plused by one.
Vicky wonders after 100 days, what is the sum of the first M numbers.
 

Input
There are multiple test cases.
First line contains a single integer T, means the number of test cases. (1T2103)
Next T line contains, each line contains one interger M.  (1M1016)
 

Output
For each test case,output the answer in a line.
 

Sample Input
 
 
3 1 3 5
 

Sample Output
 
 
1 4 7
 

Source
 
题目大意:
Vicky是个热爱数学的魔法师,拥有复制创造的能力。
一开始他拥有一个数列{1}。每过一天,他将他当天的数列复制一遍,放在数列尾,并在两个数列间用0隔开。Vicky想做些改变,于是他将当天新产生的所有数字(包括0)全加1。Vicky现在想考考你,经过100天后,这个数列的前M项和是多少?。
解题思路:
先打个表,不可能达到 100天,因为最大是10^16次方的和。。。
然后推一下每个0也就是 1 的值就行了。

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;

#define MM(a) memset(a,0,sizeof(a))

typedef long long LL;
typedef unsigned long long ULL;
const int maxn = 65;
const int mod = 1e9+7;
const double eps = 1e-10;
const int INF = 0x3f3f3f3f;
LL gcd(LL a, LL b)
{
    if(b == 0)
        return a;
    return gcd(b, a%b);
}
LL sum[maxn];
void Init()///每一个0的值
{
    sum[0] = 1;
    LL tmp = 1;
    for(int i=1; i<maxn; i++)///10^16次方最多不超过60天
    {
        sum[i] = (sum[i-1]-1)*2+tmp+1;
        tmp *= 2;
    }
}
int main()
{
    Init();
//    for(int i=0; i<63; i++)
//        cout<<sum[i]<<" ";
    int T;
    cin>>T;
    while(T--)
    {
        LL m;
        cin>>m;
        LL ret, tmp, x, y;
        ret = x = tmp = 0;
        y = 1;
        while(m)
        {
            if(m%2)
            {
                ret += sum[tmp]+x;
                x += y;
                ///cout<<ret<<endl;
            }
            y<<=1;
            tmp++;
            m>>=1;
        }
        cout<<ret<<endl;
    }
    return 0;
}


目录
相关文章
|
6月前
|
人工智能
codeforces 315 B.Sereja and Array
codeforces 315 B.Sereja and Array
19 0
|
6月前
codeforces 318 A.Even Odds B.Sereja and Array
codeforces 318 A.Even Odds B.Sereja and Array
14 0
|
6月前
codeforces 299 A. Ksusha and Array
题目就是让你找出一个数组中可以将这个数组中所有数整除的数,很明显,如果存在,这个数肯定是最小的一个。
23 0
LeetCode contest 200 5476. 找出数组游戏的赢家 Find the Winner of an Array Game
LeetCode contest 200 5476. 找出数组游戏的赢家 Find the Winner of an Array Game
|
算法 Python
LeetCode 410. Split Array Largest Sum
给定一个非负整数数组和一个整数 m,你需要将这个数组分成 m 个非空的连续子数组。设计一个算法使得这 m 个子数组各自和的最大值最小。
105 0
LeetCode 410. Split Array Largest Sum
|
机器学习/深度学习 人工智能
LeetCode之Find All Numbers Disappeared in an Array
LeetCode之Find All Numbers Disappeared in an Array
76 0
LeetCode 167:两数之和 II - 输入有序数组 Two Sum II - Input array is sorted
公众号: 爱写bug(ID:icodebugs) 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数。 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2。
943 0
[LeetCode] Find the Derangement of An Array 找数组的错排
In combinatorial mathematics, a derangement is a permutation of the elements of a set, such that no element appears in its original position.
1263 0
|
Java 索引
LeetCode Array
LeetCode数组习题 26.Remove Duplicates from Sorted Array 题目描述: Given a sorted array, remove the duplicates in ...
759 0