【递推】【HDU 2073】无限的路 (找规律)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=2073

意外之喜  还挺不错的一道题目

仔细观察不难发现   其实整个路线只有这两种线(绿色跟红色)

并且在移动过程中[x,y]的改变(红线部分)总是遵循这两种规律[x+1,y-1]或者[x-1,y+1],设x+y=z

在同一条红线中z的值是不变的

既然如此 我们不如直接用x+y来计算红线部分的总值,忽略x与y的具体数值

我们定义一个数组p1[z]来保存红线的前缀和

计算公式也很容易看出来p1[0] = 0;  p[i] = p[i-1]+i*g;     // double g = sqrt(2);   很容易看出来 第i条红线恰好有条长度为sqrt(2)的线段

那么现在红线部分就搞定了

接下来定义一个数组p2[z]来保存绿线的前缀和

同样用开平方根的方式计算第x跟绿线的长度   第x根绿线的长度为sqrt(x^2+(x+1)^2);

p2[0] = 0;  p2[i] = p[i-1]+sqrt(i^2+(i-1)^2);

此时我们只需要判断出给出的坐标点位于哪条红线上,将这条红线之前的前缀和加到sum上

再根据x的值判断这之前有多少条绿线 再将这些绿线的前缀和加到sum上

现在就只剩下最后一个问题了  即加上最后一段的距离

比如计算[3,1](距离[0,0])的长度

加上前(3+1)-1条红线 再加上前(3+1)条绿线 再加上最后一段的距离

对于[3,1],最后一段的距离是[0,4]->[1,3]->[2,2]->[3,1];(发现恰好为三个g的距离)

再尝试观察几个其他的点  发现最后一段距离恰好为x个g的距离

那么公式就出来了  sum[x,y]=p1[x+y-1]+p2[x+y]+x*g;

那么我们只需要计算一次[x1,y1]的sum,再计算一次[x2,y2]的sum进行相减取绝对值

最后的结果便出来了

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 double p1[210],p2[210];
 4 double g = sqrt(2);
 5 int main()
 6 {
 7     int i,j,x1,y1,x2,y2;
 8     double num1,num2;
 9     p1[0] = 0.0;
10     for(int i = 1;i <= 205;i++)
11         p1[i] = p1[i-1]+(i)*g;
12     p2[0] = 0;
13     for(int i = 1;i <= 205;i++)
14         p2[i] = sqrt(i*i+(i-1)*(i-1))+p2[i-1];
15     int n;
16     cin >> n;
17     while(n--)
18     {
19         scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
20         num1 = p1[x1+y1-1]+x1*g+p2[x1+y1];
21         num2 = p1[x2+y2-1]+x2*g+p2[x2+y2];
22         printf("%.3lf\n",fabs(num1-num2));
23     }
24     return 0;
25 }

原文地址:https://www.cnblogs.com/duny31030/p/8974216.html

时间: 2024-08-27 13:11:14

【递推】【HDU 2073】无限的路 (找规律)的相关文章

HDU 4572 Bottles Arrangement(找规律,仔细读题)

题目 //找规律,123321123321123321…发现这样排列恰好可以错开 // 其中注意题中数据范围: M是行,N是列,3 <= N < 2×M //则猜测:m,m,m-1,m-1,m-2,m-2,……,2,2,1,1求出前m个数字的和就是答案. //发现案例符合(之前的代码第二天发现案例都跑不对,真不知道我当时眼睛怎么了) #include <iostream> #include<stdio.h> #include<string.h> #inclu

HDU 2147-kiki&#39;s game(博弈/找规律)

kiki's game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 40000/10000 K (Java/Others) Total Submission(s): 9174    Accepted Submission(s): 5485 Problem Description Recently kiki has nothing to do. While she is bored, an idea appears in his

HDU 5703 Desert 水题 找规律

已知有n个单位的水,问有几种方式把这些水喝完,每天至少喝1个单位的水,而且每天喝的水的单位为整数.看上去挺复杂要跑循环,但其实上,列举几种情况之后就会发现是找规律的题了= =都是2的n-1次方,而且这题输出二进制数就行了......那就更简单了,直接输出1,然后后面跟n-1个0就行了╮(╯_╰)╭ 下面AC代码 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm>

HDU 5793 A Boring Question (找规律 : 快速幂+乘法逆元)

A Boring Question Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 865    Accepted Submission(s): 534 Problem Description There are an equation.∑0≤k1,k2,?km≤n∏1?j<m(kj+1kj)%1000000007=?We define

HDU - 4722 Good Numbers 【找规律 or 数位dp模板】

If we sum up every digit of a number and the result can be exactly divided by 10, we say this number is a good number. You are required to count the number of good numbers in the range from A to B, inclusive. InputThe first line has a number T (T <=

HDU 1041 Computer Transformation(找规律加大数乘)

主要还是找规律,然后大数相乘 #include<stdio.h> #include<string.h> #include<math.h> #include<time.h> #include<map> #include<iostream> #include<ctype.h> #include<string> #include<algorithm> #include<stdlib.h> #i

递推 hdu 3411

http://blog.csdn.net/wust_xhj/article/details/47779539 怎么推可以看这里 f[0]=0 f[1]=1 [0,1]* | 0  q  |(n-1)=[f(n-1),f(n)]  | 1 q-1| 跑一下快速幂 #include<stdio.h> #include<string.h> #include<algorithm> #include<math.h> using namespace std; typed

递推 HDU 2569

考虑n-2 n-1 n z[n] 代表n个块 可行方案 1  n-2 和n-1 同 3*z[n-2] 2  n-2和n-1不同 2*(z[n-1]-z[n-2]); 减一减 然后可能是其中一种 *2 z[n]=2*z[n-1]+z[n-2]; z[1]=3; z[2]=9; #include<stdio.h> #include<string.h> #include<algorithm> #include<math.h> using namespace std

HDU 4990 Reading comprehension(找规律+矩阵快速幂)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4990 Problem Description Read the program below carefully then answer the question. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include<iostream> #include

HDU 4203 博弈 打表找规律

http://acm.hdu.edu.cn/showproblem.php?pid=4203 一堆数量为s的硬币,两个人轮流从中取硬币,最后取完的人获胜,其中每次只能取k的n次方个硬币(n = 0, 1, 2, 3-),求想要取胜,当前要取走的最少硬币数. s的范围是1e9,直接储存sg函数是不现实的,所以考虑打表找找规律看. 通过打表可以得出规律: 当k为偶数时,sg函数值依次为 0 1 0 1 0 1 0 1- 当k为奇数时,sg函数值依次为 0 1 0 1 0 1-k(总长度为k + 1)