题目链接:

Array

Time Limit: 2000/1000 MS (Java/Others)   

 Memory Limit: 131072/131072 K (Java/Others)

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
 
题意:
 
一开始有一个1,每次操作都把这个数字串复制并且在前边加上一个0,然后再把得到的这个串的每个数字都加1,然后再把这个新得到的数字串加到原来串的末尾,问前m位数的和是多少;
 
思路:
 
找规律的题,先初始化,第i次操作一次得到的和为dp[i],长度为L[i],转移方程看代码;然后对一个数m,找到最大的j,L[j]<=m的长度串,ans+=dp[j],ans+=m-L[j];
m减去这个长度L[j]并去掉前导0,得到一个新的更小的m,这样回溯最后就能得到答案了;
 
AC代码:
//#include <bits/stdc++.h>
#include <vector>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio> using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<''||CH>'';F= CH=='-',CH=getchar());
for(num=;CH>=''&&CH<='';num=num*+CH-'',CH=getchar());
F && (num=-num);
}
int stk[], tp;
template<class T> inline void print(T p) {
if(!p) { puts(""); return; }
while(p) stk[++ tp] = p%, p/=;
while(tp) putchar(stk[tp--] + '');
putchar('\n');
} const LL mod=1e9+;
const double PI=acos(-1.0);
const LL inf=1e14;
const int N=1e4+;
const int maxn=; LL dp[],L[],ans,sum[];
void Init()
{
dp[]=;
L[]=;
for(int i=;i<;++i)
{
dp[i]=*dp[i-]+L[i-]+;
L[i]=*L[i-]+;
}
} void dfs(LL x)
{
if(x<=)return;
for(int i=;i<;i++)
{
if(L[i]<=x&&L[i+]>x)
{
ans+=dp[i];
x-=L[i]+;
ans+=x+;
break;
}
}
dfs(x);
}
int main()
{
int t;
read(t);
LL m;
Init();
while(t--)
{
ans=;
read(m);
dfs(m);
printf("%lld\n",ans);
} return ;
}
 
 

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 6197 array array array

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

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

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

随机推荐

  1. ASP.NET MVC- KindEditor的使用

    我用过几个EDITOR,还是比较喜欢KINDEDITOR.这个工作可能最近要用到了,周末在家花时间了解了一下.做了一下备注在这里,以备日后方便查阅. 1.首先去KINDEDITOR的官网下载最新的版本 ...

  2. C#中的Collection 2

    Core Generic interface IEnumerable<T>:you can interate my elemnts, no need to know the count, ...

  3. javaScript hook

    今天在网上搜索了不少资料,基本概念如下: 钩子(Hook),是Windows消息处理机制的一个平台,应用程序可以在上面设置子程以监视指定窗口的某种消息,而且所监视的窗口可以是其他进程所创建的.当消息到 ...

  4. table布局 height=100%无效分析

    (从死了一次又一次终于挂掉的百度空间中抢救出来的,发表日期 2014-08-11) 原文链接:http://www.cnblogs.com/gaojun/archive/2012/05/07/2487 ...

  5. 鼠标指向GridView某列显示DIV浮动列表

    需求: 当GRIDVIEW数据列过多,不方便全部显示在同一行或者一些子信息需要鼠标指向某关键列GRIDVIEW的时候显示其子信息. 设计:先把需要显示的浮动数据一次过抓取出来.而不是鼠标指向的时候才从 ...

  6. UVa11218 KTV

    // Rujia Liu // 题意:给出n个带权集合,每个集合包含1~9中的三个整数.找出其中三个集合,使得1~9恰好各出现一次,且权和最大 // 算法:暴力n^2枚举前两个集合,直接计算出第三个集 ...

  7. C#调用C++ dll时,结构体引用传参的方法

    写了一个C++的LogLog Logit 四参数等算法的接口dll,给C#调用,但是发现传参有问题 如 extern "C" _declspec(dllexport)  bool ...

  8. 《计算机问题求解》总结——2014年CCF计算机课程改革导教班(2014.07.11)

    一:引言     "心想事成".这是自己获得导教班学习机会的最佳概括.2013年年末学习李晓明老师的<人群与网络>课程:随后网络认识烟台大学贺利坚老师,了解到2013年 ...

  9. javascript之处理Ajax错误

    使用Ajax须留心两类错误.它们的差别源于视角的不同. 第一类错误是从XMLHttpRequest对象的角度看到的问题:某些因素阻止了请求发送到server,比如DNS无法解析主机名,连接请求被拒绝. ...

  10. js 获取cookie

      <!DOCTYPE html>   <html xmlns="http://www.w3.org/1999/xhtml">       <head ...