hdu 5587 Array
题目链接: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的更多相关文章
- hdu 5587 Array 数学题
Array Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5587 De ...
- hdu 5587 Array 二分
Array Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Problem ...
- HDU 5587——Array——————【规律】
Array Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Sub ...
- 2019年CCPC网络赛 HDU 6703 array【权值线段树】
题目大意:给出一个n个元素的数组A,A中所有元素都是不重复的[1,n].有两种操作:1.将pos位置的元素+1e72.查询不属于[1,r]中的最小的>=k的值.强制在线. 题解因为数组中的值唯一 ...
- HDU 5587:Array
Array Accepts: 118 Submissions: 232 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/ ...
- HDU 6197 array array array 2017沈阳网络赛 LIS
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6197 题意:给你n个数,问让你从中删掉k个数后(k<=n),是否能使剩下的序列为非递减或者非递增 ...
- hdu 6197 array array array
array array array Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 2017多校第10场 HDU 6172 Array Challenge 猜公式,矩阵幂
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6172 题意:如题. 解法: #include <bits/stdc++.h> using ...
- hdu 6197 array array array LIS
正反跑一次LIS,取最大的长度,如果长度大于n-k就满足条件. ac代码: #include <cstdio> #include <cstring> #include < ...
随机推荐
- UVALive 7138 The Matrix Revolutions(Matrix-Tree + 高斯消元)(2014 Asia Shanghai Regional Contest)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...
- mysql时间格式化,按时间段查询的MySQL语句
描述:有一个会员表,有个birthday字段,值为'YYYY-MM-DD'格式,现在要查询一个时间段内过生日的会员,比如'06-03'到'07-08'这个时间段内所有过生日的会员. SQL语句: Se ...
- LDA( Latent Dirichlet Allocation)主题模型 学习报告
1 问题描述 LDA由Blei, David M..Ng, Andrew Y..Jordan于2003年提出,是一种主题模型,它可以将文档集中每篇文档的主题以概率分布的形式给出,从而通过分析一 ...
- Linux 常用服务总结
使用linux有一段时间了,把自己在身边经常听到,使用linux经常遇到的linux常见服务总结出来,这样遇到问题会有更多的解决问题的办法,听别人摆这些专业术语时,才不会不知所云. 服务: 1.NFS ...
- OpenLayers工作原理
1.数据组织 OpenLayers通过同层(Layer)进行组织渲染,然后通过数据源设置具体的地图数据来源.因此,Layer与Source是密切相关的对应关系,缺一不可.Layer可看做渲染地图的层容 ...
- Android ps命令执行后的各项参数含义
直接输入ps后可以看到如下信息: # ps ps USER PID PPID VSIZE RSS WCHAN PC NAME root 1 ...
- 在ie与火狐的兼容性
1.在火狐下 document.getElementById("id").textContent可以获取当前获取id下的内容 而在ie下则为:document.getElem ...
- Good Bye 2015 D. New Year and Ancient Prophecy
D. New Year and Ancient Prophecy time limit per test 2.5 seconds memory limit per test 512 megabytes ...
- Redmine开发帮助
这里先零星记录二次开发用得上的知识点: 1.windows下开发环境,参考此文.最好使用rubyinstaller安装,注意选择版本.或者直接安装railsinstaller. 2.获取自定义内容,参 ...
- Monkey的ADB命令简单使用示例和解析
进行简单的压力测试: 1. adb shell monkey –p 包名 –v-v 3000 >E:\bugLog.txt -v -v 标识打印的日志的详细级别为2级,更高级有3级,也可以用1级 ...