题目链接:hdu 5587

  前两周 bc 上的题了,因为赶大作业所以没有去打,看了下官方给出的思路,感觉好强大~~竟然能转化成求二进制数 1 的个数:

  然后数位 dp 就行了,

 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef unsigned long long ull;
#define For(i,s,t) for(int i = s; i <= t; ++i) ull C[][]; // 组合数
inline void init_C(int n) {
For(i,,n) C[i][] = ;
For(i,,n) For(j,,i) {
C[i][j] = C[i - ][j - ] + C[i - ][j];
}
} // dp[i] 表示所有 i 位二进制数里'1'的个数总数,p2[i] 即 i 位二进制数的总个数
ull dp[], p2[] = {, };
inline void init(int n = ) {
init_C(n);
for(int i = ; i <= n; ++i)
for(int j = ; j <= i; ++j)
dp[i] += j * C[i][j];
For(i,,n)
p2[i] = p2[i - ] * ;
} int digit[]; // digit 数组保存的是 x 的二进制表示法
ull solve(ull x) {
int len = ;
while(x) {
digit[++len] = x % ;
x /= ;
}
int num1 = ; // num1 记录的是 digit 里第 i 位前面的'1'的个数
ull res = ;
for(int i = len; i; --i) {
// 只处理第 i 位为'1'的情况就行,'0'的话在该位没有比它更小的了,所以不用处理
if(digit[i] == ) {
// p2[i - 1] * num1 统计的是第 i 位前面的'1'的个数总数(因为此时第 i 位取'0',后面的 i-1 位可以任取)
// dp[i - 1] 统计后面 i-1 位里'1'的个数总数
res += p2[i - ] * num1 + dp[i - ];
++num1;
}
}
return res;
} int main() {
int t;
ull m;
init();
scanf("%d",&t);
while(t--) {
scanf("%I64u",&m);
printf("%I64u\n",solve(m + )); // 因为上面统一处理的是比 x 小的数,所以在这里 +1
}
return ;
}

  网上好像有很多思路可以递归或者递推找规律的,我学习了下,感觉也很强大:

 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef unsigned long long ull;
const int N = ;
const ull maxLen = 1e16 + ; ull preLen[N], preSum[N]; inline void init(int n = ) {
preLen[] = preSum[] = ;
for(int i = ; i <= n; ++i) {
preLen[i] = preLen[i - ] * + ;
preSum[i] = preSum[i - ] * + (preLen[i] - preLen[i - ]);
if(preLen[i] > maxLen) return ;
}
} ull dfs(ull mlen) {
if(mlen == ) return ;
int pos;
for(int i = ; i <= ; ++i) {
if(mlen < preLen[i + ]) {
pos = i;
break;
}
}
ull remainLen = (mlen == preLen[pos] ? : mlen - preLen[pos] - );
return preSum[pos] + dfs(remainLen) + (mlen - preLen[pos]);
} int main() {
init();
int t;
ull m;
scanf("%d",&t);
while(t--) {
scanf("%I64u",&m);
printf("%I64u\n",dfs(m));
}
return ;
}

  因为不是比赛时做,所以时间比较充裕,变量名写得有点长了,看着像在写工程代码

  这题,必须好好琢磨才行,它和刚过去的 2015CCPC 南阳的热身赛的第一题很像,都是递归的思想,当时现场想不出来,却被身旁的师弟秒掉了,好惭愧的感觉 -_-||   看来自己还要勤加练习啊,弱渣就需多努力~

  顺便贴一下 5586 Sum 的代码,自己1A掉的:

 #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = ;
#define For(i,s,t) for(int i = s; i <= t; ++i) int a[N], b[N]; inline int trans(const int &x) {
return ( * x + ) % ;
} int maxSum(int *b, int n) {
int res = b[], cur = b[];
for(int i = ; i <= n; ++i) {
cur = max(cur + b[i], b[i]);
res = max(res, cur);
}
return max(res, );
} int main() {
int n;
while(~scanf("%d",&n)) {
int sum = ;
For(i,,n) {
scanf("%d",a + i);
sum += a[i];
b[i] = trans(a[i]) - a[i];
}
printf("%d\n", sum + maxSum(b, n));
}
return ;
}

hdu 5587 Array的更多相关文章

  1. hdu 5587 Array 数学题

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

  2. hdu 5587 Array 二分

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

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

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

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

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

  5. HDU 5587:Array

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

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

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

  7. hdu 6197 array array array

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

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

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

  9. hdu 6197 array array array LIS

    正反跑一次LIS,取最大的长度,如果长度大于n-k就满足条件. ac代码: #include <cstdio> #include <cstring> #include < ...

随机推荐

  1. 导出Excel And 导出word

      <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx. ...

  2. windows 下编辑shell,到linux报错,也是windows换行等造成

    sed -i 's/\r$//' shell.sh 然后linux 下就不会报语法错误了.

  3. phonegap android 输入法弹出会遮盖表单框的解决办法

    phonegap android 当页面内容比较多,表单超出屏幕范围时,点击输入,输入法会遮盖住表单框,而且无法向上滑动. 经过测试发现,是由于config.xml中设置了 FullScreen 的全 ...

  4. WCF学习目的

    WCF,window communication Foundation,是微软推出的面向服务应用的一款产品. 近来为一个WEB app前端项目写后台接口.涉及到跨域访问,服务代理等方面的内容.项目的需 ...

  5. C# 如何获取当前应用程序的上一级路径

    Server.MapPath 的使用方法 用法: 1.Server.MapPath ("/") 应用程序根目录所在的位置 如 C:\Inetpub\wwwroot\ 2.Serve ...

  6. c# 动态调用.net编写的webservices接口

    创建类WebServiceHelper: public class WebServiceHelper { #region 动态调用WebService动态调用地址 /// < summary&g ...

  7. mysql 5.7.16 远程连接

    1登录到mysql中,为root进行远程访问的授权,所以主机都可以链接 GRANT ALL PRIVILEGES ON . TO 'root'@'%'IDENTIFIED BY 'mypassword ...

  8. clover 在win10下工作不正常

    1. 右键兼容性, 选win8 2. 文件夹选项:在同一个窗口中打开每个文件夹

  9. css+div解决文字溢出控制显示字数

    一.一般的文字截断(适用于内联与块):  Example Source Code [www.mb5u.com] .text-overflow {display:block;/*内联对象需加*/widt ...

  10. Links for Introduction To Calculus

    The links to download the material for the course Introduction To Calculus are provided in the follo ...