codeforces The Wall - 题解

Iahub and his friend Floyd have started painting a wall. Iahub is painting the wall red and Floyd is painting it pink. You can consider the wall being made of a very large number of bricks, numbered 1, 2, 3 and
so on.

Iahub has the following scheme of painting: he skips x?-?1 consecutive bricks, then he paints the x-th
one. That is, he‘ll paint bricks x, 2·x, 3·x and
so on red. Similarly, Floyd skips y?-?1 consecutive bricks, then he paints the y-th
one. Hence he‘ll paint bricks y, 2·y, 3·y and
so on pink.

After painting the wall all day, the boys observed that some bricks are painted both red and pink. Iahub has a lucky number a and Floyd has
a lucky number b. Boys wonder how many bricks numbered no less than a and
no greater than b are painted both red and pink. This is exactly your task: compute and print the answer to the question.

Input

The input will have a single line containing four integers in this order: xyab.
(1?≤?x,?y?≤?1000, 1?≤?a,?b?≤?2·109,a?≤?b).

Output

Output a single integer — the number of bricks numbered no less than a and no greater than b that
are painted both red and pink.

Sample test(s)

input

2 3 6 18

output

3

这是一道简单题,也隔了一段时间没做简单题目了。

这次感觉又不一样了,可以很快就能写出很优雅的代码了,故此很想贴贴自己的代码。

优雅代码的关键就是要利用数学的思想去解:

本题的实质是可以转化为求最大公倍数的的问题,然后利用Inclusion-exclusion(包含和不包含)的原则,计算有多少个数能被a除尽这个公倍数,有多少个数能被b除尽这个公倍数,然后相减就得到最终答案了。

#include <stdio.h>
using namespace std;

int TheWallGCD(int a, int b)
{
	while (b)
	{
		int t = b;
		b = a % b;
		a = t;
	}
	return a;
}

void TheWall340A()
{
	int x, y, a, b;
	scanf("%d %d %d %d", &x, &y, &a, &b);

	int r = TheWallGCD(x, y);
	r = x / r * y;

	int m = a % r? 0 : 1;

	printf("%d", b / r - a / r + m);
}

codeforces The Wall - 题解,布布扣,bubuko.com

时间: 2024-10-11 05:23:46

codeforces The Wall - 题解的相关文章

codeforces Flipping Game 题解

Iahub got bored, so he invented a game to be played on paper. He writes n integers a1,?a2,?...,?an. Each of those integers can be either 0 or 1. He's allowed to do exactly one move: he chooses two indices i and j (1?≤?i?≤?j?≤?n) and flips all values 

codeforces Gravity Flip 题解

Little Chris is bored during his physics lessons (too easy), so he has built a toy box to keep himself occupied. The box is special, since it has the ability to change gravity. There are n columns of toy cubes in the box arranged in a line. The i-th

codeforces A. TL 题解

Valera wanted to prepare a Codesecrof round. He's already got one problem and he wants to set a time limit (TL) on it. Valera has written n correct solutions. For each correct solution, he knows its running time (in seconds). Valera has also wrote m 

Codeforces Unlucky Ticket 题解

Each of you probably has your personal experience of riding public transportation and buying tickets. After a person buys a ticket (which traditionally has an even number of digits), he usually checks whether the ticket is lucky. Let us remind you th

codeforces A. k-String 题解

A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string

codeforces A. Array题解

Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold: The product of all numbers in the first set is less than zero (?<?0). The product of all numbers in the secon

codeforces A. Cakeminator 题解

本题思路: 1 先扫描行,如果可以吃,就数吃了多少格,然后做好标志 2 扫描列,同样处理 扫描完就可以出答案了. 时间效率是O(n*m)了.算是暴力法 题目: http://codeforces.com/problemset/problem/330/A #include <vector> #include <algorithm> #include <utility> #include <string> #include <queue> #incl

Codeforces Amusing Joke 题解

So, the New Year holidays are over. Santa Claus and his colleagues can take a rest and have guests at last. When two "New Year and Christmas Men" meet, thear assistants cut out of cardboard the letters from the guest's name and the host's name i

codeforces Epic Game 题解

Simon and Antisimon play a game. Initially each player receives one fixed positive integer that doesn't change throughout the game. Simon receives number a and Antisimon receives number b. They also have a heap of nstones. The players take turns to m