摘要:DescriptionAoshu is very popular among primary school students. It is mathematics, but much harder than ordinary mathematics for primary school students. Teacher Liu is an Aoshu teacher. He just comes out with a problem to test his students:Given a serial of digits, you must put a '=' and no
阅读全文
摘要:地址:http://acm.hdu.edu.cn/showproblem.php?pid=1042思路:大数阶乘代码如下: 1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 #define N 40000 5 int s[N]; 6 int main() 7 { 8 int i,j,k,t,n; 9 while(scanf("%d",&n)!=EOF)10 {11 memset(s,0,sizeof(s));//每次使用前都要清零 12 s[0]=1;
阅读全文
摘要:地址:http://acm.hdu.edu.cn/showproblem.php?pid=2159思路:动态规划dp之二维完全背包问题状态方程是关键。。。。。/*dp[j][l] = Max(dp[j][l],dp[j-1][l-b[i]]+a[i])它表示 用掉了l点的忍耐度,并且杀了j个怪后,所获得的最大经验数。*/ 题目分析:二维费用的背包问题是指:对于每件物品,具有两种不同的费用;选择这件物品必须同时付出这两种代价;对于每种代价都有 一个可付出的最大值(背包容量)。问怎样选择物品可以得到最大的价值。设这两种代价分别为代价1和代价2,第i件物品所需的两种代价分别为a[i]和 b[i]。两
阅读全文
摘要:地址:http://acm.hdu.edu.cn/showproblem.php?pid=1114思路:动态规划之完全背包问题题目分析:给出了钱罐开始的重量e和装满后的重量f,然后给你n种硬币,每个价值为p,重量为w,求出最小的价值使钱罐的重量恰好为w如果不存在 输出This is impossible.状态转移方程:f[v]=min{f[v],f[v-w[i]]+p[i]}注意:一道简单的完全背包题(于0-1背包就是第二个for循环倒过来就行了),要求做的仅仅是求最小值,而不是最大值,那么只要对初始化进行一些改变就可以了。把f[0]赋值为0,其他赋值为无穷大。这个题目的意思是有一只存钱的小猪
阅读全文
摘要:地址:http://acm.hdu.edu.cn/showproblem.php?pid=2066思路:求有多个起点和多个终点,找出从其中任意一个起点到任意一个终点的距离最短,用floyd算法,但是要注意很多细节的优化,要不会超时借鉴代码如下: 1 #include<cstdio> 2 #include<cstring> 3 #include<cstdlib> 4 #define inf 0x3fffffff 5 int map[1001][1001],max; 6 bool start[1001],end[1001]; 7 int floyd() 8 {
阅读全文
摘要:地址:http://acm.hdu.edu.cn/showproblem.php?pid=2602思路:dp问题之01背包代码如下:一维的,我习惯的作风。。。 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #define N 1001 5 int dp[N]; 6 int c[N],w[N]; 7 int max(int x,int y) 8 { 9 return x>y?x:y;10 }11 int main()12 {13 int t,n,v,i,j;14 scanf
阅读全文
摘要:地址:http://acm.hdu.edu.cn/showproblem.php?pid=1421思路:动态规划dp借鉴代码如下: 1 #include<cstring> 2 #include<iostream> 3 #include<algorithm> 4 #define P(x,y) ((x-y)*(x-y)) 5 using namespace std; 6 int a[2010],d[2010][2010]; 7 int main() 8 { 9 int i,j,n,k;10 while(cin>>n>>k){11 for(
阅读全文
摘要:地址:http://acm.hdu.edu.cn/showproblem.php?pid=2544思路:(dijkstra算法)代码如下: 1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 #define Max 0xfffffff 5 int m[10010][10010],p[10010]; 6 bool vis[10010]; 7 int vexnum,arcnum; 8 void dijstra() 9 {10 int i,j,k,t,min;11 memset(vis,0
阅读全文
摘要:地址:http://acm.hdu.edu.cn/showproblem.php?pid=1142思路1:题目大意是给你一个图。起点为1,终点为2然后点a到点b是合法的判断(Notice 1:)是当b存在一个到终点的距离小于a到终点的最小距离,求从起点到终点的路径数。Notice 1:不合法并非是断路,合法指回家的时候只按dis[b] < dis[a]的路线走、dis[]表示到终点2的最短路径, 满足dis[b] < dis[a], 表示这样a->b的是可选择的。 就是说每次回家时选择从距起点(最短距离)近的点 向 距起点(最短距离)远的点走, 求这样的路线条数。dp[i]
阅读全文
摘要:地址:http://acm.hdu.edu.cn/showproblem.php?pid=1372思路:求“马”从一点到另一点的最短距离,马走日,BFS即可分析:广度优先搜索题题意如图所示:一个棋子(骑士)可以有八个方向走,广搜确定最小的走的步数。代码如下: 1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <queue> 5 using namespace std; 6 int c[9][9]; 7 int dir[8][2] = {{-2,-
阅读全文
摘要:地址:http://acm.hdu.edu.cn/showproblem.php?pid=1274不太懂,,先发表别人的。。。慢慢研究。。。。算法:用了最直接,最暴力的方法。一层层展开。从右到左,遇到(或是数字就展开。一开始想了很多,想得很复杂,但都没办法想下去。于是,就用最简单的方法,直接AC了。原以为是超时的。幸好数据不是很大。用了两个字符数组,一个用于存重复内容。一个存后辍具体做法。统一性。1。没括号的,数字+字母,字母这两种类型。当成数字+(字母),(字母)类型。重复内容,直接是单个字母。2。没数字的,字母,(字符串),当成1+字母,1+(字符串)类型。重复次数是1次。这样就把上面的所
阅读全文
摘要:1 #include<stdio.h> 2 int main() 3 { 4 int n,i,m,sum; 5 scanf("%d",&n); 6 while(n--) 7 { 8 scanf("%d",&m); 9 sum=3;10 for(i=1;i<=m;i++)11 sum=(sum-1)<<1;12 printf("%d\n",sum);13 }14 return 0;15 }16
阅读全文
摘要:1 #include<stdio.h> 2 int main() 3 { 4 int n,i,m; 5 int f[50]; 6 scanf("%d",&n); 7 while(n--) 8 { 9 scanf("%d",&m);10 f[1]=0;f[2]=1;f[3]=2;11 for(i=4;i<=m;i++)12 f[i]=f[i-1]+f[i-2];13 printf("%d\n",f[m]);14 }15 return 0;16 }
阅读全文
摘要:地址:http://acm.hdu.edu.cn/showproblem.php?pid=2045一开始就把式子弄出来了,可惜还是WA一次,忘记验算了n=3了,这个式子的范围是n>3才行。思路如下:f(n) = 1, ... , n-2 , n-1, n前n-2个已涂好后,涂第n-1个即有2种情况: 1. n-1的色与n-2和1的色都不相同,那么n就是剩下的那个色,米选择。 即就是f(n-1)2. n-1的色与n-2不相同但与1个色一样,那么n的色就有2个色选择. 即就是f(n-2)*2 综上得:f(n) = f(n-1) + 2*f(n-2); 别忘了验算得出n的范围。正确代码:...
阅读全文
摘要:地址:http://acm.hdu.edu.cn/showproblem.php?pid=2044正确: 1 #include<iostream> 2 using namespace std; 3 int main() 4 { 5 long long f1,f2,temp; 6 int n,a,b; 7 cin>>n; 8 while(n--) 9 {10 cin>>a>>b;11 f1=1;f2=1;12 for(int i=1;i<b-a;i++)13 {14 t...
阅读全文
摘要:地址:http://acm.hdu.edu.cn/showproblem.php?pid=2013思路:没啥说的。。。 1 #include<stdio.h> 2 int main() 3 { 4 int m,x; 5 while(scanf("%d",&m)!=EOF) 6 { 7 x=1; 8 while(--m) 9 x=(x+1)<<1;10 printf("%d\n",x);11 }12 return 0;13 }
阅读全文
摘要:地址:http://acm.hdu.edu.cn/showproblem.php?pid=1272思路:并查集的运用当用find函数去查找祖先时,如果合并过程中查找到的祖先相同,那么说明有两条路可以连通,就不符合要求 1 #include<string.h> 2 #include<stdio.h> 3 int f[100010];//存放一个节点的父节点的编号 4 bool is[100010];//标志编号为I的房间是否存在 5 void exist(int x,int y)//判断x,y点是否已经存在 6 { 7 if(!is[x]){ 8 is...
阅读全文
摘要:地址:http://acm.hdu.edu.cn/showproblem.php?pid=2574思路1:素数有关 ,不太懂。。。思路2:因为 大于 1 << 16 的和数都能用 1 -- 1<<16 之间的素数表示, 不能表示的肯定是 素数了, 所以处理 1-- 1<<16之间的素数就可以了.不过貌似这题的数据很弱没有大于 1 << 16 的素数. 1 #include<stdio.h> 2 #include<string.h> 3 #include<math.h> 4 int prime[1000000]
阅读全文
摘要:地址:http://acm.hdu.edu.cn/showproblem.php?pid=1999思路1:标准的筛选法。求出每个数的因子和,然后看因子和是否在1000以内,是的话就证明等于因子和的这个数不是不可摸数。 1 #include<stdio.h> 2 #include<string.h> 3 #include<math.h> 4 int sum[1000001],sign[1001]; //sum是因子数和,开的空间大些 5 int main() 6 { 7 int n,num,i,j; 8 scanf("%d",&n)
阅读全文
摘要:地址:http://acm.hdu.edu.cn/showproblem.php?pid=1232思路:并查集的运用,最小生成树并查集的精髓(即它的三种操作,结合实现代码模板进行理解):1、Make_Set(x) 把每一个元素初始化为一个集合n初始化后每一个元素的父亲节点是它本身,每一个元素的祖先节点也是它本身(也可以根据情况而变)。2、Find_Set(x) 查找一个元素所在的集合n查找一个元素所在的集合,其精髓是找到这个元素所在集合的祖先!这个才是并查集判断和合并的最终依据。判断两个元素是否属于同一集合,只要看他们所在集合的祖先是否相同即可。合并两个集合,也是使一个集合的祖先成为另一个集合
阅读全文