博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 5587 Array 数学题
阅读量:6889 次
发布时间:2019-06-27

本文共 1565 字,大约阅读时间需要 5 分钟。

Array

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=5587

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

HINT

 

题意

题解:

数学题(:

首先我们先用找规律算出第i天一共能得到的和是多少:g(i)=g(i-1)+2^(i-1)

然后我们就可以递归求解了,不断地利用2的幂来进行递归

代码:

#include
#include
#include
#include
#include
using namespace std;vector
Q;long long ans = 0;long long g[230];void solve(long long k){ if(k<=0)return; int p = --upper_bound(Q.begin(),Q.end(),k)-Q.begin(); ans+=g[p]+k-Q[p]; solve(k-Q[p]-1);}int main(){ long long T=2; Q.push_back(0); for(int i=1;i<=59;i++) { Q.push_back(T-1);//2^i-1 T*=2; } g[1]=1;long long tmp=2; for(int i=2;i<=Q.size();i++) { g[i]=2*g[i-1]+tmp;//存的是第i天的答案 tmp=tmp*2; } int t;scanf("%d",&t); for(int i=1;i<=t;i++) { long long x;scanf("%I64d",&x); ans = 0; solve(x); printf("%I64d\n",ans); }}

 

转载地址:http://fetbl.baihongyu.com/

你可能感兴趣的文章
java的强制类型转换想到的
查看>>
简要介绍cookie与session的区别与联系
查看>>
mysql flush用法
查看>>
response.setHeader()的用法
查看>>
一位前辈的经验,给正在思考的自己
查看>>
分享一篇关于lucene原理的文章
查看>>
基于 HTML5 结合互联网+ 的 3D 隧道
查看>>
Win10下 80端口被system(pid=4)占用的解决方法
查看>>
使用SubVersion+TortoiseSVN多仓库方式进行版本控制
查看>>
Nginx虚拟目录alias和root目录
查看>>
MySQL(Extends)
查看>>
Android KeyboardView实现App内置键盘开发
查看>>
Python文件夹复制
查看>>
细谈 vue 核心- vdom 篇
查看>>
ajax+springmvc实现跨域请求
查看>>
SaltStack快速入门-配置管理
查看>>
批处理研究(QQ绿化和卸载)
查看>>
对比农行与建行网银业务办理流程
查看>>
Oracle 11G RAC 安装图示(一)
查看>>
【xpghost】xp系统启动后迟延问题如何解决
查看>>