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 #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\n",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 }

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. 日常Java 2021/11/2

    Java提供了一种对象序列化的机制,该机制中,一个对象可以被表示为一个字节序列,该字节序列包括该对象的数据.有关对象的类型的信息和存储在对象中数据的类型.将序列化对象写入文件之后,可以从文件中读取出来 ...

  2. mybatis-plus分页记坑

    mapper接口方法返回IPage,如果不传page会报npe,底层assert page!=null有啥用?

  3. YYYY-MM-DD引发的问题

    yyyy 和 YYYY 用YYYY格式化代码 2019-12-31 转 YYYY/MM/dd 格式: 2020/12/31 2020-01-01 转 YYYY/MM/dd 格式: 2020/01/01 ...

  4. Java Web 实现Mysql 数据库备份与还原

    前段时间某某删库事故付出的惨重代价告诉我们: 数据备份的必要性是企业数据管理极其重要的一项工作. 1. Mysql备份与还原命令 备份命令: mysqldump -h127.0.0.1 -uroot ...

  5. gitlab之数据备份恢复

    备份#备份的时候,先通知相关人员服务要听 ,停止两个服务,并影响访问 root@ubuntu:/opt/web1# gitlab-ctl stop unicorn ok: down: unicorn: ...

  6. 快速上手git gitlab协同合作

    简单记录,整理. 摘要 为方便大家快速上手Git,并使用Gitlab协同合作,特编写此手册,手册内容不会太丰富与深入.主要包含如下内容: Git 使用教程1.1 安装1.2 常用命令1.3 版本控制1 ...

  7. 监控Linux服务器网站状态的SHELL脚本

    1,监控httpd状态码的shell脚本代码. #!/bin/sh #site: www.jquerycn.cn # website[0]=www.jquerycn.cn/chuzu/' #网站1 m ...

  8. react-native安卓运行报错:The number of method references in a .dex file cannot exceed 64K.

    错误原因:App里面方法数超过64K解决方法:在android/app/build.gradle中添加implementation 'com.android.support:multidex:1.0. ...

  9. SQL查询:并集、差集、交集

    新建两个表进行测试: test_a ID name 1 曹操 2 郭嘉 3 孙权 4 周瑜 test_b ID name 1 刘备 2 关羽 3 张飞 4 孙权 5 周瑜 1.UNION形成并集 UN ...

  10. 【C/C++】C++ warning: control reaches end of non-void function return

    控制到达非void函数的结尾. 一些本应带有返回值的函内数到容达结尾后可能并没有返回任何值. 这时候,最好检查一下是否每个控制流都会有返回值. 我是ostream声明的时候没有写return out; ...