cf题解--E. Fractions

https://codeforces.com/group/5yyKg9gx7m/contest/277432/problem/E

E. Fractions

About 44 days are left before College Scholastic Ability Test is held. This exam aims to measure students' achievement of National Curriculum standards and scholastic ability required for college education. (http://www.kice.re.kr/sub/info.do?m=0205&s=english)

One of the subjects covered by this test is Mathematics, which consists of 21 multiple choice questions and 9 short-answer questions. The answer of each short-answer question is guaranteed to be a unique positive integer below 1 000, as you can see from the answer sheet below.


However, the organizers might want to give students short-answer questions with non-integer answers, such as 23–√ or 53. Usually, the workaround is to write the answer in a canonical form, and then sum up all the integers inside that form and ask students to write that number instead.

In particular, when the answer is a positive rational number ab, the organizers usually ask students to reduce it and sum up the numerator and the denominator of the reduced fraction. For example, when the answer is 1810, the student should reduce it to 95 and write the final answer as 9+5=14.


However, when the answer is 521500, the reduced fraction is also 521500, so the student should write the final answer as 521+500=1021. But this shouldn't happen, since all the answers for the short-answer questions are below 1 000. To avoid this situation, the organizers should make sure that after reducing the fraction, the sum of the numerator and the denominator shouldn't exceed 999. Let's call such fractions as Suneung Fractions. For example, 19962 and 1810 are Suneung fractions, while 19982 and 521500 are not.

Suppose that, this year, one of the organizers wrote a problem, and the answer to that problem is xy. Since the problem is not finalized yet, the only thing we know is A≤x≤B and C≤y≤D holds, for given A,B,C,D. The organizers want to know, among all the pairs (x,y), how many of xy is a Suneung fraction. Write a program that counts this number.

Input
The first and only line contains four space-separated integers A,B,C and D (1≤A≤B≤1012, 1≤C≤D≤1012)

Output
Print the number of integral pairs (x,y) (A≤x≤B, C≤y≤D), where xy is a Suneung fraction.

Examples
inputCopy
5 8 3 6
outputCopy
16
inputCopy
2018 2019 2018 2019
outputCopy
2

题目描述:

题目说了一大堆,就是问,给定A≤x≤B, C≤y≤D,x/y化成最简时他们的和x+y小于1000有多少个(x,y)解。

分析:

化成最简,就是gcd(x,y)==1,因为x+y小于1000,所以遍历1~1000所有满足的情况。

对于每个最简x,y情况,我们可以把x和y同时扩大一定的倍数。这样化简后还是x,y。那么只要计算出每个x,y在(A,B)和(C,D)范围内任意扩大,有多少个满足的解,全部加和就是答案。因为x,y要同时扩大,计算就是min(B/x,D/y)-max((A-1)/x,(C-1)/y)。(A和C减1是因为它是闭区间)

因为对于每个最简的(x,y)扩大后设为(X,Y),银行他要化简,最终一定会回到(x,y),而不会变成其他的解。所以无论怎么扩大,都不会和其他解扩大的情况重复。

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
#include<climits>
#include<vector>
#include<queue>
#include<map>
#include<stack>
using namespace std;
typedef long long ll;
const int maxn=1e3+6;
int main()
{
    ll A,B,C,D,ans=0;
    cin>>A>>B>>C>>D;
    A--,C--;//因为是闭区间,所以包含了A和C
    for(int i=1;i<1000;i++)
    {
        for(int j=1;i+j<1000;j++)
        {
            if(__gcd(i,j)==1) 
            {
                ll a=min(B/i,D/j)-max(A/i,C/j);
                if(a<0) a=0;
                ans+=a;
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/studyshare777/p/12765913.html