zoukankan      html  css  js  c++  java
  • hdu-5587 Array(递归)

    Array

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


    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
     
    Recommend
    hujie
     
    Statistic | Submit | Discuss | Note

    Hangzhou Dianzi University Online Judge 3.0
    Copyright © 2005-2016 HDU ACM Team. All Rights Reserved.
    Designer & Developer Wang Rongtao LinLe GaoJie GanLu
    Total 0.000000(s) query 1, Server time : 2016-01-02 14:06:15, Gzip enabled

    规律题;

    思路:可以写一下前几项的式子。

    1. 1
    2. 112
    3. 1121223
    4. 112122312232334
    5. ......

    那么求把每项的和的递推公式就是a[n]=2*a[n-1]+2n-1.

    每项有2n-1个数,然后打表。打到2n-1<=1e16;

    然后递归求解。复杂度为log(n);

     1 #include<stdio.h>
     2 #include<algorithm>
     3 #include<iostream>
     4 #include<string.h>
     5 #include<stdlib.h>
     6 const long long N=1e16;
     7 typedef long long ll;
     8 void  ss(ll n,ll k);
     9 ll a[60];
    10 ll sum=0;
    11 using namespace std;
    12 int main(void)
    13 {
    14     ll i,j,k,p,q,M;
    15     a[1]=1;
    16     for(i=2; (ll)1<<i<=N; i++)
    17     {
    18         a[i]=a[i-1]*2+((ll)1<<(i-1));
    19 
    20     }//打表(ll)1强制将1转为64位的。不然会爆
    21     scanf("%lld",&k);//k值可求得为54;
    22     while(k--)
    23     {
    24         sum=0;
    25         scanf("%lld",&M);
    26         ss(M,0);
    27         printf("%lld
    ",sum);
    28     }
    29     return 0;
    30 
    31 }
    32 void  ss(ll n,ll k)
    33 {
    34     ll i,j,p,q;
    35     for(i=1; i<=54; i++)
    36     {
    37         if(n==((ll)1<<(i))-1)//如果所求的位数正好是上面打表中an的某一项就是对应那项加上项数*翻的倍数。
    38         {
    39             sum+=a[i]+k*(((ll)1<<i)-1);
    40             return ;
    41         }
    42         else if(((ll)1<<(i))-1>n)//找前面的比n小的an
    43         {
    44             break;
    45         }
    46 
    47     }
    48     ll pp=k*(((ll)1<<(i-1))-1)+a[i-1]+k+1;//前一项an和(k+1)中间多加的0那个点对应的此时的值。
    49     sum+=pp;
    50     ll qq=n-((ll)1<<(i-1));//剩下后面没求位数
    51     if(qq==0)
    52     {
    53         return ;
    54     }
    55     ss(qq,k+1);
    56 }
    油!油!you@
  • 相关阅读:
    华为OD机试 :找终点
    华为OD机试 :磁盘容量排序
    剑指Offer-从上到下打印二叉树
    GO语言学习笔记3-int与byte类型转换
    剑指Offer-树的子结构
    LeetCode :21.合并两个有序链表
    LeetCode :206.反转链表
    剑指Offer-删除链表的结点
    剑指Offer-调整数组顺序使奇数位于偶数前面
    如何创建ts+react项目
  • 原文地址:https://www.cnblogs.com/zzuli2sjy/p/5006591.html
Copyright © 2011-2022 走看看