题目链接 题意:n(n<=300)个球,每个球上标有一个标号(a[i]<=300),从中拿一个,不放回,再拿一个,问第一个球上的数字严格大于第二个球上的数字的概率。 思路:古典概型。总数为n*(n-1)/2…然后标号最大300,不妨用cnt[i]统计标号为i的球的个数。从小往大扫一遍cnt,cnt[i]对分子的贡献就是cnt[i]*cur。。cur 为 sum{cnt[1]..cnt[i-1]}; 最后注意将分子除以2,因为有一半是第一个球比第二个球小的情况。
题目链接 题意:给出n个点,m条边,定义一条路径的价值为【路径长度*(路径终点的度)】,求最大价值。 思路:一月份的时候写过一个回溯。。。TLE22了。。。其实也能猜到是dp..但是无奈不会写。然而其实真的不难== 我们枚举路径的终点,dp[i]表示以点i为终点能得到的最长路径长度。
题目链接 题意:n个点,m(m<n/2)条不能走的边,问最少连多少条边,使得任何两个点之间的距离最多为2. 输出最少的边数和连接的哪些边。 思路: **数据范围很关键。**数据范围很关键。数据范围很关键。
题目链接 题意:给出n和k,给出一个长度为n的字符串表示房间的占用情况(0表示没占用,1表示已占用),从n个房间中找出k+1个,使得k+1中的k个距离k+1个中的1个的距离和最小。
题目链接 题意:长度为n的初始为1,2,3…n的序列,最多进行k次两个数交换,变换后的序列中最懂能有多少逆序对。 思路:贪心得想。。每次变最外层的对答案贡献最多。 以及,最能能变化n/2次。
题目链接 题意:2×2的格子,有三个位置分别放”A“ “B” “C” ,一个位置为空。只有和空位相邻位置上的字母能移动到空位。没有其他移动规则。现在给出两个状态。问能否互相转化。 思路: 貌似可以dfs…?但是一共才2*2,可以直接暴力枚举。 手写一种变换最多能有12种。
题目链接 题意:设条件A为一个数恰好是k个互不相同的b的整数次幂的和,问某一个区间内满足条件A的数的个数是有多少个。
Example. Let X=15, Y=20, K=2, B=2. By this example 3 numbers are the sum of exactly two integer degrees of number 2:
17 = 24+20, 18 = 24+21, 20 = 24+22.
题目链接 题意:将一个10进制数x按照2进制展开后得到的值设为f(x)…现在给出a,b(10^9)问【0,b】中满足f[x]<=f[a]的数的个数。 思路:先算出f[a],然后我们发现f(x)最大也就10*2^10=10240,数组可以存下,搞之。和上一道题类似,我们不关心两个f函数的值具体是多少,只关心它们的相对大小情况,所以还是可以合并成一个变量。然而我用两个变量为什么错了,不懂==
hdu5642题目链接 题意:问长度为n的仅由26个小写字母组成的合法字符串有多少个。如果某个字符连续出现四次或以上,则这个字符串为非法。否则为合法。
思路:当时以为是组合数学的题。。。推了半天公式还是还是gg… 现在学了数位dp..果然是数位dp里很简单的一种。。。 dp[i][j][k]表示长度为i,最后一个字符对应的数字为j,最后一个字符出现了k次的方案数。
题目链接 题意:找到某区间中平衡数的个数。所谓平衡数是指,存在某个位置,使得两边的力矩相等。举个例子,比如14326,如果把4作为中间,那么左边=11=1,右边=31+22+62=19。 思路:枚举中间的pivot,注意个位数也是平衡数(就是认为两边的力矩都是0了),所以每一个位置都可能是平衡位置,枚举的时候从1到len… 一开始我是分别记录两边的值,非常浪费空间,然而发现其实没必要。我们只关心左右是否相等,而不关心左右的值到底是多少,所以可以把两边的值带符号合并成一个值(pivot左边为+,pivot右边为负)。如果最后为0,说明左右相等。
题目链接 题意:问某区间中,round number 的个数是多少。所谓round number,当且仅当一个数的二进制表示中,‘0’的个数大于等于‘1’的个数。 思路:简单数位dp..和windy数那道题类似,都是不允许前导0.。。所以在dfs中要加一维判断前面是否有非0的数。。。
题目链接 题意:如果一个整数符合下面3个条件之一,那么我们就说这个整数和7有关—— 1、整数中某一位是7; 2、整数的每一位加起来的和是7的整数倍; 3、这个整数是7的整数倍;
题目链接 题意:给出n,问[1,n]中,满足包含“13”且这个数(不是各位的和)能被13整除的数的个数。 思路:依然是数位dp..不过有一个小tip。。
由于包含13的情况非常难考虑(包含一个“13”,两个“13”…..)
题目链接 题意:求一个区间内所有位数字之和能被10整除的数的个数。 思路:数位dp,dfs要一个参数记录从最高位到现在的pos位置的数字之和的结果。 代码实现 1 dp[i][j] 表示长度为i,和为j的方案数。 2 记得开long long ,然而我开了那么多long long 忘了dp 的long long 结果wa到死。。果然大早上不清醒吗== 3 4 5 6/* *********************************************** 7Author :111qqz 8Created Time :2016年03月16日 星期三 08时10分19秒 9File Name :code/hdu/4722.cpp 10************************************************ */ 11 12#include <cstdio> 13#include <cstring> 14#include <iostream> 15#include <algorithm> 16#include <vector> 17#include <queue> 18#include <set> 19#include <map> 20#include <string> 21#include <cmath> 22#include <cstdlib> 23#include <ctime> 24#define fst first 25#define sec second 26#define lson l,m,rt<<1 27#define rson m+1,r,rt<<1|1 28#define ms(a,x) memset(a,x,sizeof(a)) 29typedef long long LL; 30#define pi pair < int ,int > 31#define MP make_pair 32 33using namespace std; 34const double eps = 1E-8; 35const int dx4[4]={1,0,0,-1}; 36const int dy4[4]={0,-1,1,0}; 37const int inf = 0x3f3f3f3f; 38LL l,r; 39int digit[30]; 40LL dp[30][15]; //dp 数组忘记开long long ,wa到死。。。。。。。。。日了哈士奇。 41LL dfs ( int pos,int sum,bool limit) 42{ 43 if (pos==0) 44 { 45 if (sum==0) return 1; 46 else return 0; 47 } 48 if (!limit&&dp[pos][sum]!=-1) return dp[pos][sum]; 49 50 int mx = limit?digit[pos]:9; 51 52 LL res = 0 ; 53 for ( int i = 0 ; i <= mx; i ++) 54 { 55 res+=dfs(pos-1,(sum+i),limit&&i==mx); 56 } 57 58 if (!limit) dp[pos][sum] = res; 59 60 return res; 61 62} 63LL solve ( LL n) 64{ 65// if (n==0) return 1; 66 // if (n<=9) return 0; 67 if (n<0) return 0; 68 ms(digit,0); 69 int len = 0 ; 70 while (n) 71 { 72 digit[++len] = n % 10; 73 n /= 10; 74 } 75 76 return dfs(len,0,true); 77} 78int main() 79{ 80 #ifndef ONLINE_JUDGE 81 freopen("code/in.txt","r",stdin); 82 #endif 83// ios::sync_with_stdio(false); 84 int T; 85 cin>>T; 86 ms(dp,-1); 87 int cas = 0 ; 88 while (T--) 89 { 90 scanf("%lld %lld",&l,&r); 91 LL ans = solve (r)-solve(l-1); 92 93 printf("Case #%d: %lld\n",++cas,ans); 94 } 95 96 #ifndef ONLINE_JUDGE 97 fclose(stdin); 98 #endif 99 return 0; 100}
题目链接 题意:不含前导零且相邻两个数字之差至少为2的正整数被称为windy数。 windy想知道,在A和B之间,包括A和B,总共有多少个windy数? 思路:数位dp 这道题的特点是前面不允许前导0,也就是说,如果第i位前面全是0的话,这个数就变成了i位数,i就变成了最高位,而最高位没有前面的数(**如果这里不考虑不允许前导0这个因素而把前面的一个数认为成是0就错了) **最高位的数可以直接取。 还有记忆化调用以及存储的时候也要注意…只有当位数相同的时候转移才有意义。 具体的方法是dfs中多了一个prehasnonzero的bool变量,就是字面意思,判断当前位置前面的位置是够存在一个非0的值。
题目链接 题意:问从1到n的所有数中,有多少个数含有数字串“49” 思路:和上一道不要62很像,但是由于是要统计有49的,但是有49的情况实在太多了,正难则反,用减法定理反过来考虑,先统计出不含49的数的个数,这样就和不要62一样了,然后再用总数减。
题目链接 题意:问区间[n,m]中,不含数字4,也不含数字串“62”的所有数的个数。
思路:可以转化成求区间[0,x]
第一次接触数位dp,参考了这几篇博客。
不要62(数位dp)解题报告
解题报告2
解题报告3
比较重要的前提:
题目链接 题意:求曼哈顿距离和平方根距离相等的点的对数? 思路:化简发现是绝对值乘积等于0,容斥搞搞。
代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2016年03月07日 星期一 18时43分02秒 4File Name :code/C.cpp 5************************************************ */ 6 7#include <cstdio> 8#include <cstring> 9#include <iostream> 10#include <algorithm> 11#include <vector> 12#include <queue> 13#include <set> 14#include <map> 15#include <string> 16#include <cmath> 17#include <cstdlib> 18#include <ctime> 19#define fst first 20#define sec second 21#define lson l,m,rt<<1 22#define rson m+1,r,rt<<1|1 23#define ms(a,x) memset(a,x,sizeof(a)) 24typedef long long LL; 25#define pi pair < int ,int > 26#define MP make_pair 27 28using namespace std; 29const double eps = 1E-8; 30const int dx4[4]={1,0,0,-1}; 31const int dy4[4]={0,-1,1,0}; 32const int inf = 0x3f3f3f3f; 33const int N=2E6+7; 34 35struct node 36{ 37 int x,y; 38 39 bool operator < (node b)const 40 { 41 if (x==b.x) return y<b.y; 42 return x<b.x; 43 } 44}p[N]; 45 46 47 48bool cmp( node a,node b) 49{ 50 return a.y<b.y; 51} 52LL cal( LL x) 53{ 54 LL res = x*(x+1)/2; 55 return res; 56} 57int n; 58LL a,b,c; 59LL cnta,cntb,cntc; 60int main() 61{ 62 #ifndef ONLINE_JUDGE 63 freopen("code/in.txt","r",stdin); 64 #endif 65 66 ios::sync_with_stdio(false); 67 cin>>n; 68 for ( int i = 1 ;i <= n ; i++) cin>>p[i].x>>p[i].y; 69 70 sort(p+1,p+n+1); 71 72 a=b=c=0LL; 73 cnta=cntb=cntc=0LL; 74 75 76 for ( int i = 1 ; i <= n-1 ; i++) 77 { 78 if (p[i].x==p[i+1].x) 79 { 80 cnta++; 81 } 82 else 83 { 84 a+=cal(cnta); 85 cnta = 0 ; 86 } 87 } 88 a +=cal(cnta); 89 90 91 for ( int i = 1 ; i <= n-1 ; i++) 92 { 93 if (p[i].x==p[i+1].x&&p[i].y==p[i+1].y) 94 { 95 cntc++; 96 } 97 else 98 { 99 c +=cal(cntc); 100 cntc = 0 ; 101 } 102 } 103 104 c+=cal(cntc); 105 106 107 108 sort(p+1,p+n+1,cmp); 109 for ( int i = 1 ; i <= n-1 ; i ++) 110 { 111 if (p[i].y==p[i+1].y) 112 { 113 cntb++; 114 } 115 else 116 { 117 b +=cal(cntb); 118 cntb = 0 ; 119 } 120 } 121 b +=cal(cntb); 122 123 LL ans=0LL; 124 ans = a+b-c; 125 cout<<ans<<endl; 126 127 #ifndef ONLINE_JUDGE 128 fclose(stdin); 129 #endif 130 return 0; 131}
题目链接 题意:给出一个数列,按照最好的策略排序使得a[i+1]>a[i]的对数尽可能多,问最多的对数是多少。 思路:类似计数排序?
代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2016年03月07日 星期一 17时06分48秒 4File Name :code/cf/#345/B.cpp 5************************************************ */ 6 7#include <cstdio> 8#include <cstring> 9#include <iostream> 10#include <algorithm> 11#include <vector> 12#include <queue> 13#include <set> 14#include <map> 15#include <string> 16#include <cmath> 17#include <cstdlib> 18#include <ctime> 19#define fst first 20#define sec second 21#define lson l,m,rt<<1 22#define rson m+1,r,rt<<1|1 23#define ms(a,x) memset(a,x,sizeof(a)) 24typedef long long LL; 25#define pi pair < int ,int > 26#define MP make_pair 27 28using namespace std; 29const double eps = 1E-8; 30const int dx4[4]={1,0,0,-1}; 31const int dy4[4]={0,-1,1,0}; 32const int inf = 0x3f3f3f3f; 33const int N=1E3+7; 34int n ; 35int a[N]; 36int cnt[N]; 37int num[N]; 38int sum[N]; 39int main() 40{ 41 #ifndef ONLINE_JUDGE 42 freopen("code/in.txt","r",stdin); 43 #endif 44 45 cin>>n; 46 ms(cnt,0); 47 ms(num,0); 48 ms(sum,0); 49 for ( int i = 1 ; i <= n ; i++) 50 { 51 cin>>a[i]; 52 cnt[a[i]]++; 53 } 54 if (n==1) 55 { 56 puts("0"); 57 return 0 ; 58 } 59 if (n==2) 60 { 61 if (a[1]==a[2]) 62 { 63 puts("0"); 64 } 65 else 66 { 67 puts("1"); 68 } 69 return 0; 70 } 71 72 int mx = -1; 73 for ( int i = 1 ; i <= 1000 ; i++) 74 { 75 num[cnt[i]]++; 76 mx = max(mx,cnt[i]); 77 } 78 79 for ( int i = mx ; i >= 1 ; i --) 80 { 81 sum[i] = sum[i+1]+num[i]; 82 } 83 84 int ans = 0 ; 85 for ( int i = 1 ; i <= mx ; i++) 86 { 87 ans +=sum[i]-1; 88// cout<<"sum[i]:"<<sum[i]<<endl; 89 } 90 cout<<ans<<endl; 91 92 93 94 #ifndef ONLINE_JUDGE 95 fclose(stdin); 96 #endif 97 return 0; 98}
题目链接 题意:两个手柄? 初始的电量给出,只有一个充电器,每经过一秒,充着电的手柄电量增加1,没有充电的手柄电量减少2,允许电量充到0以上,当有电量为0的时候,或者当某一分钟开始的时候有手柄电量为1,游戏立即结束。问最多能玩多少时间游戏。