Skip to main content
  1. Posts/

uva 846 Steps

·2 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid;=8&page;=show_problem&problem;=787

题意:从x增加到y,第一步和最后一步步长只能是1,其他步一定可以是上一步减一,和上一步相等,或者上一步步长加一,三种情况,且步长恒为正。问从x到y最少需要的步数。

思路:首先可以知道,走的最快的方法是1+2+3+…+k+…+3+2+1.这个式子的结果是一个完全平方数,为k^2,式子的长度为2*k-1.即为答案。 我们可以知道k肯定不超过 ceil(sqrt(y-x)).但是中间的k是不一定要加的。再判断k^2减去k是否已经达到结果,如果是,就将答案减一。 注意对于这种做法x=y是特殊情况。。需要特判。。。。

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年01月28日 星期四 19时57分44秒
 4File Name :code/uva/846.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;
33int x,y;
34int main()
35{
36	#ifndef  ONLINE_JUDGE
37	freopen("code/in.txt","r",stdin);
38  #endif
39
40	int T;
41	cin>>T;
42	while (T--)
43	{
44	    scanf("%d %d",&x,&y);
45            if (x==y) {cout<<0<<endl;continue;}
46	    int k = ceil(sqrt(y-x));
47
48	    int sum = k*(k-1);
49	    int ans =  2*k-1;
50	    if (y-x<=sum) ans--;
51
52	    cout<<ans<<endl;  //x,y相等的情况要特判
53
54	}
55
56  #ifndef ONLINE_JUDGE
57  fclose(stdin);
58  #endif
59    return 0;
60}

还有一种做法是比较好想的非数学方法。 由于对第一步和最后一步的步长有要求,都是1. 很容易想到从两边往中间走。 然后每走两步(两个方向各一步),后增加一次步长。

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年01月28日 星期四 19时57分44秒
 4File Name :code/uva/846.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;
33int x,y;
34int main()
35{
36	#ifndef  ONLINE_JUDGE
37	freopen("code/in.txt","r",stdin);
38  #endif
39
40	int T;
41	cin>>T;
42	while (T--)
43	{
44	    scanf("%d %d",&x,&y);
45	    int step = 1;
46	    int len = y-x;
47	    int x = 0;
48	    int ans = 0 ;
49	    while (len>0)    //思路关键:从两边往中间走。
50	    {
51		len-=step;
52		ans++;
53		if (x) step++;
54		x ^=1;
55	    }
56	    cout<<ans<<endl;
57
58
59
60	}
61
62  #ifndef ONLINE_JUDGE
63  fclose(stdin);
64  #endif
65    return 0;
66}

Related

uva 10025 The ? 1 ? 2 ? ... ? n = k problem

·1 min
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=966 题意:?1?2?3?4…?n=k,把每个?替换成+或者-,找到最小的n使得式子成立。 题意:这道题最关键的一点是。如果s1=1+2+3+.,x+..+n>=k (所有数取正数),那么一定有s2=1+2+3+..-x+..+n=k

uva 113 Power of Cryptography

·1 min
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid;=8&page;=show_problem&problem;=49 题意:求p开n次方。保证结果为整数。 思路:p最大10的101次方。。。double最大10的308次方。。因为肯定是整数。。不存在精度问题。。所以可以用douible水过QAQ…

hdoj 5605 || bc #68 div 2 1001 geometry

·1 min
题意:geometry Accepts: 324 Submissions: 622 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 在平面直角坐标系上有一个点PP, 他的坐标是(x, y)(x,y). 有一条直线y = kx + by=kx+b经过了PP, 且分别交x, yx,y正半轴于A, BA,B. 求|PA| * |PB|∣PA∣∗∣PB∣的最小值. 输入描述 第一行一个TT, 表示数据组数. 接下来TT行每行两个正整数x,yx,y, 表示PP的坐标.

cf 611 B ||codeforces goodbye 2015 B. New Year and Old Property (数学或者数位dp)

http://codeforces.com/contest/611/problem/B 题意:问a到b(1E18),二进制表示中只有一个0的数有多少个。 思路:这么大的数。。。不是有循环节就是math problems. UD:20160318讲道理还有可能是数位dp好不好。。。 我们发现可以很容易得算出1到x的二进制表示中只有一个0 的数有多少个。

codeforces #332 div 2 D. Spongebob and Squares

·2 mins
http://codeforces.com/contest/599/problem/D 题意:给出总的方格数x,问有多少种不同尺寸的矩形满足题意,输出方案数和长宽(3,5和5,3算两种) 思路:比赛的时候gg了。。其实稍微在纸上推一下。就会得到对于n,m的矩形,一共会有-nnn+3nnm+n+3n*m的方格。数量级是n3。 我们可以实际跑一遍。发现对于x1E18的数量级,n不会超过1442550,1E6,可以搞。