zoukankan      html  css  js  c++  java
  • HDU 5587 Array

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5587

    Array

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


    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
     

    题目大意:输入数字M,输出前M项和。

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <stdlib.h>
    
    using namespace std;
    
    long long sum[1000];
    long long num[1000];
    
    void Init()
    {
        sum[1]=1;
        num[1]=1;
        for(int i=2;i<=60;i++)
        {
            num[i]=num[i-1]*2+1;
            sum[i]=sum[i-1]*2+num[i-1]+1;
        }
    
    }
    
    long long Find(long long M)
    {
        if(M<=0)
            return 0;
        int k=lower_bound(num+1,num+55,M)-num;
    
        if(num[k]==M)
            return sum[k];
        else
            return Find(M-num[k-1]-1)+sum[k-1]+M-num[k-1];
    
    }
    
    int main()
    {
        int t;
        long long M;
        Init();
        scanf("%d",&t);
        while(t--)
        {
            scanf("%lld",&M);
            long long ans=Find(M);
            printf("%lld
    ",ans);
        }
    
        return 0;
    }
  • 相关阅读:
    Unique Binary Search Trees 解答
    Unique Paths II 解答
    Unique Paths 解答
    Maximum Subarray 解答
    Climbing Stairs 解答
    House Robber II 解答
    House Robber 解答
    Valid Palindrome 解答
    Container With Most Water 解答
    Remove Duplicates from Sorted List II 解答
  • 原文地址:https://www.cnblogs.com/mengzhong/p/5007230.html
Copyright © 2011-2022 走看看