`
ScoJack
  • 浏览: 863 次
  • 性别: Icon_minigender_1
  • 来自: 广州
最近访客 更多访客>>
社区版块
存档分类
最新评论

Codeforces Round #224 Div.2 Problem.B--Number Busters

阅读更多

  原题如下:

B. Number Busters
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Arthur and Alexander are number busters. Today they've got a competition.

Arthur took a group of four integers a, b, w, x (0 ≤ b < w, 0 < x < w) and Alexander took integer с. Arthur and Alexander use distinct approaches to number bustings. Alexander is just a regular guy. Each second, he subtracts one from his number. In other words, he performs the assignment: c = c - 1. Arthur is a sophisticated guy. Each second Arthur performs a complex operation, described as follows: if b ≥ x, perform the assignment b = b - x, if b < x, then perform two consecutive assignments a = a - 1; b = w - (x - b).

You've got numbers a, b, w, x, c. Determine when Alexander gets ahead of Arthur if both guys start performing the operations at the same time. Assume that Alexander got ahead of Arthur if c ≤ a.

Input

The first line contains integers a, b, w, x, c (1 ≤ a ≤ 2·109, 1 ≤ w ≤ 1000, 0 ≤ b < w, 0 < x < w, 1 ≤ c ≤ 2·109).

Output

Print a single integer — the minimum time in seconds Alexander needs to get ahead of Arthur. You can prove that the described situation always occurs within the problem's limits.

 

  这道题的来历在题目有中已经说明了。初看这道题觉得蛮简单的。就是模拟两人的计算过程,其中有个小小的分类讨论。啪啪啪迅速代码实现之后还稳稳地把给出来的数据都测试了一遍,都通过了。于是立马提交。可是在第五个测试数据的时候超时了!这才回过头来仔细想,如果a,c的数据给的过于极端(a = 1, c = pow(10, 9)*2 的话,就要计算超过pow(10, 9)*2次,这显然超时了!也就是说,这个题目有更加简单的算法,这个算法的首要条件是不可对计算过程进行模拟!

  于是,我改变思路,去求解模拟计算次数n的数学表达式。得到n >= ((c-a) * w - b) / (w-x);这个式子。

  后来,在提交失败后,又找到了这个式子的限制条件,n > 0。并且解决了将整型数据向上取的问题。才把这个小题给AC了。

  从此题得到的经验是:1、小题目有大技巧(直接找关于答案n的数学表达式)

                                      2、得到数学表达式后不要大喜过望,直接提交,要找找其限制条件,否则就要看到大红的WA!

                                     3、下次遇到这种题目首先要考虑的是应该用什么样类型的算法,将不可能的算法排除掉(本次如果模拟计算过程的话,遇到极端数据绝对超时,不能保证该算法的正确性)

  下面附上本人代码。

#include<stdio.h>
#include<stdlib.h>
main()
{
	__int64 a, c;
	double b, w, x;
	scanf("%I64d%lf%lf%lf%I64d", &a, &b, &w, &x, &c);
	long long n;
	double p;
	if(a < c){
		p = ((c-a) * w - b) / (w-x);	
		n = p;
		double d = p - n;
		if(d > 0)
			n += 1;
	}		
		else
		n = 0;
	printf("%I64d\n", n);	
}

 

分享到:
评论

相关推荐

    Codeforces Round #723 (Div. 2).md

    Codeforces Round #723 (Div. 2).md

    Codeforces Round #629 (Div. 3) B. K-th Beautiful String

    B. K-th Beautiful String 题目链接-B. K-th Beautiful String 题目大意 长度为n的字符串包含n−2n−2n−2个aaa和222个bbb,求按照字典序排列输出第kkk个字符串 解题思路 第一个bbb在倒数第二位有1个字符串,在...

    Codeforces Round #627 (Div. 3) B. Yet Another Palindrome Problem

    就是把所有相等的数放到一个vector里,如果他出现大于2次,看最远的间距是否大于2即可,找到一个就可以 代码: #include #include #include #include #include #include #include #include #include #include #...

    Codeforces Round #630 (Div. 2) D. Walk on Matrix(构造)

    上面代码跑出来的dp[n][m]是0,然后从(1,1)(1,2)(2,2)(2,3)这样的相与值是k (看懂ans+k是啥应该就懂了) 代码: int main() { std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0); int ans=(1&...

    Codeforces Round #627 (Div. 3) C. Frog Jumps(思维)

    传送门 题意: 开始位置在0,问能否跳到n+1位置 每步只能跳d 在1——n每个位置有方向,L,R,求d的最小值 思路: 只用找相邻两个R之间的最大值即可 ...#define rep(i,a,b) for(int i=a;i=b;i--) typedef long long l

    Codeforces Round #479 (Div. 3) E. Cyclic Components

    E. Cyclic Components 题目链接-E. Cyclic Components 题目大意 给你nnn个点和mmm条边,求所构成图中单圈环的个数 ...并查集并查集并查集 很明显单圈环每个点的度都为222,所以我们可以用数组cnt[]记录每个点的度,...

    Codeforces Round #629 (Div. 3) E.Tree Queries (DFS)

    Codeforces Round #629 (Div. 3) E.Tree Queries (DFS) 思路:若ai 在路径上 ,则ai的父结点一定在路径上,若ai是路径上某个结点的子结点,则ai的父结点一定在路径上,综上只需考虑ai的父节点就行了。对每个ai判断...

    Codeforces Round #627 (Div. 3)B. Yet Another Palindrome Problem

    B. Yet Another Palindrome Problem 题目链接-B. Yet Another Palindrome Problem 题目大意 给一个长为n(≤5000)的数组,问是否存在一个长度至少为3的子序列是回文的,子序列的数可以不连续但是相对顺序不可变 ...

    Codeforces Round #628 (Div. 2)

    给两两节点放一个数字(0~n-2 唯一) 给你一棵树,求所有任意两节点相连的路以外的路上的数字的最小值最小 思路 构造 若一个点连了三条边及以上,则这个点的边从最小值开始赋值。其他边从最大点开始赋值。 证明:一...

    Codeforces Round #628 (Div. 2) A. EhAb AnD gCd

    输入一个正整数x,找出这样的2个正整数a和b,使得gcd(a,b)+lcm(a,b)=x 解题思路 找最特殊的情况a=1,b=x-1即可 这样a,b两个数最大公因数为1,最小公倍数x-1,满足题意√ 附上代码 #include #define int long long #...

    Codeforces Round #628 (Div. 2)【A B C D】

    传送门 A. EhAb AnD gCd 直接输出1,n-1即可 #include #include #include #include ...#define rep(i,a,b) for(int i=a;i=b;i--) typedef long long ll; using namespace std; const int MAXN=1e5+50; con

    Codeforces Round #627 (Div. 3) A. Yet Another Tetris Problem

    给一个长度为n的数组,两种操作,一个是把任意一个ai变成ai+2a_i变成a_i+2ai​变成ai​+2,另一个是如果所有数都大于0,可以把所有数减1,问通过这些操作能否把所有数变为0 思路: 如果任意两个数之差为奇数,那么就...

    Codeforces Round #620 (Div. 2) Longest Palindrome

    B. Longest Palindrome time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output Returning back to problem solving, Gildong is now studying about ...

    【Codeforces Round#620 (Div. 2)】B. Longest Palindrome 题解

    题目链接:B. Longest Palindrome 题目 Returning back to problem solving, Gildong is now studying about palindromes. He learned that a palindrome is a string that is the same as its reverse. For example...

    Codeforces Round #635 (Div. 2)D. Xenia and Colorful Gems

    题解:6nlogn,先sort三个数组a,b,c, 六次枚举二分查找,再每次min找最小值,例如:先固定数组a,再在数组b,c中利用lower_bound找到第一个大于等于a[i]的数, #pragma GCC optimize(2) #include #define ll long long...

    Codeforces Round #617 (Div. 3), problem: (B) Food Buying

    放在B类仍然是比较水的,标签“math”,解题思路就是每次剩余个位数的钱不花,这样就能保证每次都会找回来&gt;=1的钱款,用于下一次购物。可以使用迭代的方法,每次迭代需要更新已花钱总数和剩余钱款总数,最后剩余的钱...

    Codeforces Round #633 (Div. 2) B. Sorted Adjacent Differences(排序,思维)

    -2,4,5,5,6,8 要输出的序列应该是每次从前面选一个,然后从后面选一个 -2,8,4,6,5,5 然后把该序列倒着输出即可 代码: #include #include #include #include #include #include #include #include #...

    Codeforces Round #628 (Div.2) C.Ehab and Path-etic MEXs(树,思维)

    传送门 题意: 给一颗n个结点的数,然后n-1条边,我们要做的...如果不是一条链,那肯定有个结点的度大于等于3,把这个结点周围的三条边分别给值0,1,2,这样所有MEX(u,v)最大值为2,因为不可能有一条边同时经过0,1,2这三

    Codeforces Round #628 (Div. 2) A~~D

    A #include using namespace std; typedef long long ll; int main(){ int t; cin&gt;&gt;t; while(t--){ ll x; cin&gt;&gt;x; cout&lt;&lt;1&gt;&gt;t; while(t--){ st.clear(); ll n; cin &gt;&gt;n;... ll re

Global site tag (gtag.js) - Google Analytics