[蓝桥杯2019初赛]数列求值
给定数列1, 1, 1, 3, 5, 9, 17, …,从第4 项开始,每项都是前3 项的和。求
第20190324 项的最后4 位数字。
注意:
- 遇到取后n位数,则%1000...(后面跟n个零);
- 取后几位数字的时候,注意判断位数、输出前导零。
答案:4659
AC代码:
1 #include<string.h> 2 #include3 #include 4 #include 5 #include 6 #include 7 #include
WA代码:(不知道这样写为什么不对??)
1 #include<string.h> 2 #include3 #include 4 #include 5 #include 6 #include 7 #include
[蓝桥杯2019初赛]数的分解
把2019分解成3个各不相同的正整数之和,并且要求每个正整数都不包含数字2和4,一共有多少种不同的分解方法?
注意交换3个整数的顺序被视为同一种方法,例如1000+1001+18 和1001+1000+18 被视为同一种。
思路: i,j,k相加等于2019,接着把满足条件的存入数组中进行排序、标记判断即可。
注意:题看清楚,题面上给的所有要求都必须在代码中体现出来(仔细检查啊大哥!!!)
像这种交换顺序题意说明还算是同一种的,可以利用排序进行判断,之后再开一个二维数组进行标记就可以嘞。
AC代码:
1 #include<string.h> 2 #include3 #include 4 #include 5 #include 6 #include 7 #include
[蓝桥杯2019初赛]立方和
注意:定义的数据范围啊,,,
int的2019的立方溢出了,需要改数据类型,老记不住到时候怎么错的都不知道,这么简单的也错,唉
(ull)i*i*i=2019*2019*2019
AC代码:
1 #include<string.h> 2 #include3 #include 4 #include 5 #include 6 #include 7 #include
POJ2135-Farm Tour(最小费用最大流模板题)
题面:
When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 <= N <= 1000) fields numbered 1..N, the first of which contains his house and the Nth of which contains the big barn. A total M (1 <= M <= 10000) paths that connect the fields in various ways. Each path connects two different fields and has a nonzero length smaller than 35,000. To show off his farm in the best way, he walks a tour that starts at his house, potentially travels through some fields, and ends at the barn. Later, he returns (potentially through some fields) back to his house again. He wants his tour to be as short as possible, however he doesn't want to walk on any given path more than once. Calculate the shortest tour possible. FJ is sure that some tour exists for any given farm. Input * Line 1: Two space-separated integers: N and M. * Lines 2..M+1: Three space-separated integers that define a path: The starting field, the end field, and the path's length. Output A single line containing the length of the shortest tour.
样例:
Sample Input 4 5 1 2 1 2 3 1 3 4 1 1 3 2 2 4 2
Sample Output 6
题意:
给出n个点,m条边,问一个人从家走到barn的最短长度(设家1,barn为N);
所以,问的就是 去:1->N 和 返:N->1 两条路径的最短长度,两条路径不能重复;
所以,问题变成了:有两个人,从1->N,它们所走路径不能重复(走的边不能重复),求最短长度。
所以变成了最小费用最大流问题,自己再自己想想。
思路:
我以为是个最短路。。但是最短路貌似好像不能解决路径重复问题叭。。
妥妥的模板,我终于碰到了,原来我先做的都是变形题,我太难了。。
题意转化到算法上还是比较不好想到,怎么分析在上面的题意里面已经写清楚了。
容量cap传入的为1或者2(自己判断传入什么),花费cost即点和点之间的权值。
建边最重要:
memset(head,-1,sizeof(head)); tot=0; s=0,t=n+1; for(int i=1;i<=m;i++) { int u,v,w; scanf("%d %d %d",&u,&v,&w); add(u,v,1,w); add(v,u,1,w); } add(s,1,2,0); add(n,t,2,0); printf("%d\n",MCMF());
AC代码:
1 #include<string.h> 2 #include3 #include 4 #include 5 #include 6 #include 7 #include
待解决:
上面的数列求值中,为什么这样处理可以,
for(int i=5;i<=20190324;i++) { a[i]=a[i-1]+a[i-2]+a[i-3]; if(a[i]>10000) a[i]%=10000; }
这样处理就不对,而且开ll和int出来结果也不一样,从后面溢出???可是不取余输出前面几位也不是正确答案啊???
for(int i=5;i<=20190324;i++) a[i]=a[i-1]+a[i-2]+a[i-3]; int w=a[20190324]%10000;