Luo Gu p1458 solution to a problem

Sorry, you who do not know how Konjac yanxiujie insert a link qwq had to stick text of the qwq: https://www.luogu.org/problemnew/show/P1458

Yes, it is a yellow question, because the question qwq your little konjac just so difficult

ok, come on

This question, after reading the title, that Italy should be relatively easy to understand;

Meaning of the title is given a number,

Allows you to find the denominator in this range and the entire number of scores less than or equal to a fraction of all, and also pay attention to when the output is to have order.

Then we can direct enumeration, determine and record.

First, we enter directly after the for loop to enumerate the two can find all find out scores

for ( int I = 0 ; I <= n-; I ++ )
         for ( int J = . 1 ; J <= n-; J ++ ) 
             IF (I <= J) { // Dear Sirs, there is a determination condition, as long as the molecule large denominator that we can elect him score 
                int ROM = gcd (i, J);
                 if (ROM == 1 ) { // In fact, if the two can merge together is leaving only the second 
                    head ++ ; 
                    NUM [head] .x = I; 
                    NUM [head] .y = J; 
                    NUM [head] .mal = I * 1.0 / j * 1.0;
                } 
                else if(rom != 1) {
                    head++;
                    num[head].x = i / rom;
                    num[head].y = j / rom;
                    num[head].mal = (i / rom * 1.0) / (j / rom * 1.0);
                } 
            }

function determining the greatest common divisor gcd

int gcd(int x,int y) {
    return y == 0 ? x : gcd(y, x % y);
}

Sorted according to size fractions

int cmp(zero x,zero y) {
    return x.mal < y.mal;
}

Finally, clean AC codes (personal preference)

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int n, head;
struct zero{
    int x, y;
    double mal;
}num[100000];
int gcd(int x,int y) {
    return y == 0 ? x : gcd(y, x % y);
}
int cmp(zero x,zero y) {
    return x.mal < y.mal;
}
int main() {
    scanf("%d",&n);
    for(int i = 0; i <= n; i++)
        for(int j = 1; j <= n; j++) 
            if(i <= j) {
                int rom = gcd(i, j);
                if(rom == 1) {
                    head++;
                    num[head].x = i;
                    num[head].y = j;
                    num[head].mal = i * 1.0 / j * 1.0;
                } 
                else if(rom != 1) {
                    head++;
                    num[head].x = i / rom;
                    num[head].y = j / rom;
                    num[head].mal = (i / rom * 1.0) / (j / rom * 1.0);
                } 
            }
    sort(num + 1, num + 1 + head,  cmp);
    for(int i = 1; i <= head; i++) 
        if(num [i] .mala! = num [i + 1 ] .mala) 
            printf ( " % d /% d \ n " , whether [i] .x, whether [i] .y);
    return  0 ; 
}

Thanks for watching ~ ~ ~

Guess you like

Origin www.cnblogs.com/yanxiujie/p/11145132.html