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.(1≤T≤2∗103)
Next T line contains, each line contains one interger M. (1≤M≤1016)
 
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

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

规律题;

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

  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. 1 #include<stdio.h>
  2. 2 #include<algorithm>
  3. 3 #include<iostream>
  4. 4 #include<string.h>
  5. 5 #include<stdlib.h>
  6. 6 const long long N=1e16;
  7. 7 typedef long long ll;
  8. 8 void ss(ll n,ll k);
  9. 9 ll a[60];
  10. 10 ll sum=0;
  11. 11 using namespace std;
  12. 12 int main(void)
  13. 13 {
  14. 14 ll i,j,k,p,q,M;
  15. 15 a[1]=1;
  16. 16 for(i=2; (ll)1<<i<=N; i++)
  17. 17 {
  18. 18 a[i]=a[i-1]*2+((ll)1<<(i-1));
  19. 19
  20. 20 }//打表(ll)1强制将1转为64位的。不然会爆
  21. 21 scanf("%lld",&k);//k值可求得为54;
  22. 22 while(k--)
  23. 23 {
  24. 24 sum=0;
  25. 25 scanf("%lld",&M);
  26. 26 ss(M,0);
  27. 27 printf("%lld\n",sum);
  28. 28 }
  29. 29 return 0;
  30. 30
  31. 31 }
  32. 32 void ss(ll n,ll k)
  33. 33 {
  34. 34 ll i,j,p,q;
  35. 35 for(i=1; i<=54; i++)
  36. 36 {
  37. 37 if(n==((ll)1<<(i))-1)//如果所求的位数正好是上面打表中an的某一项就是对应那项加上项数*翻的倍数。
  38. 38 {
  39. 39 sum+=a[i]+k*(((ll)1<<i)-1);
  40. 40 return ;
  41. 41 }
  42. 42 else if(((ll)1<<(i))-1>n)//找前面的比n小的an
  43. 43 {
  44. 44 break;
  45. 45 }
  46. 46
  47. 47 }
  48. 48 ll pp=k*(((ll)1<<(i-1))-1)+a[i-1]+k+1;//前一项an和(k+1)中间多加的0那个点对应的此时的值。
  49. 49 sum+=pp;
  50. 50 ll qq=n-((ll)1<<(i-1));//剩下后面没求位数
  51. 51 if(qq==0)
  52. 52 {
  53. 53 return ;
  54. 54 }
  55. 55 ss(qq,k+1);
  56. 56 }

hdu-5587 Array(递归)的更多相关文章

  1. hdu 5587 Array

    题目链接:hdu 5587 前两周 bc 上的题了,因为赶大作业所以没有去打,看了下官方给出的思路,感觉好强大~~竟然能转化成求二进制数 1 的个数: 然后数位 dp 就行了, #include< ...

  2. hdu 5587 Array 数学题

    Array Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5587 De ...

  3. hdu 5587 Array 二分

    Array Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem ...

  4. HDU 5587——Array——————【规律】

    Array Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Sub ...

  5. 2019年CCPC网络赛 HDU 6703 array【权值线段树】

    题目大意:给出一个n个元素的数组A,A中所有元素都是不重复的[1,n].有两种操作:1.将pos位置的元素+1e72.查询不属于[1,r]中的最小的>=k的值.强制在线. 题解因为数组中的值唯一 ...

  6. HDU 5587:Array

    Array  Accepts: 118  Submissions: 232  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 131072/ ...

  7. HDU 6197 array array array 2017沈阳网络赛 LIS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6197 题意:给你n个数,问让你从中删掉k个数后(k<=n),是否能使剩下的序列为非递减或者非递增 ...

  8. hdu 1005 1021 递归超限 找规律 // 只要看题中n较大都是有规律的

    因为n>1000000000所以用递归 数组超限, 由递归函数f(n)=(A*f(n-1)+B*f(n-2))%7; 因为是除7的余数 因次一共有7*7=49种情况, 以后的值都和之前的对应相等 ...

  9. hdu 6197 array array array

    array array array Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  10. 2017多校第10场 HDU 6172 Array Challenge 猜公式,矩阵幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6172 题意:如题. 解法: #include <bits/stdc++.h> using ...

随机推荐

  1. 小程序https启用tls1.2

    公司的web服务器是iis7,在开发微信小程序的时候,需要启用TLS1.2. 将下面的代码复制到文本,存为reg文档,双击搞定. Windows Registry Editor Version 5.0 ...

  2. Ubuntu16.04安装 2.8.5版本Ansible

    wget https://bootstrap.pypa.io/pip/2.7/get-pip.py && python get-pip.py pip install --upgrade ...

  3. navicate连接Mysql5.7时,显示Access denied for user 'root'@'localhost' (using password: YES) 错误

    最近新装了Mysql5.7,按如下设置好了允许远程连接    (1)找到mysql配置文件并修改 sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf 将bind-ad ...

  4. nodejs-Express框架

    JavaScript 标准参考教程(alpha) 草稿二:Node.js Express框架 GitHub TOP Express框架 来自<JavaScript 标准参考教程(alpha)&g ...

  5. 一道题目学ES6 API,合并对象id相同的两个数组对象

    var arr2=[{id:1,name:'23'}] var arr1=[{id:1,car:'car2'}] const combined = arr2.reduce((acc, cur) =&g ...

  6. [PE结构]导出表结构浅析

    导出函数的总数-->以导出函数序号最大的减最小的+1,但导出函数序号是可自定义的,所以NumbersOfFunctions是不准确的 1.根据函数名称找,函数名称表->对应索引函数序号表中 ...

  7. 【二分答案】CF1613 C. Poisoned Dagger

    题目:Problem - C - Codeforces 本题的优解是二分答案,但我其实不会二分,本质是用了两个指针作为边界,然后不断对半缩小范围来快速确定答案. 神奇的二分法 代码: #include ...

  8. SpringMVC responseBody注解分析

    @responsebody表示该方法的返回结果直接写入HTTP response body中一般在异步获取数据时使用,在使用@RequestMapping后,返回值通常解析为跳转路径,加上@respo ...

  9. shell脚本下载网页图片

    和大家分享一个shell脚本写的图片抓取器.使用方法:img_downloader.sh.使用时在shell下输入:./img_downloader.sh www.baidu.com -d image ...

  10. 【科研】科研【合同】盖章流程/横向&#183;非涉密/电子科技大学

    [前置手续] 一.在科研管理系统里填单子,立项. 二.科研管理系统审核通过后,对于对面给好的合同,在合同系统里选择[合同业务发起-发起非标准合同],填单子. 三.会有一系列的审核,审核完成后打印合同备 ...