Hackerrank-pairs-again (violence + pretreatment)

Source: Internet
Author: User
Tags time limit
Topic Links:

Https://www.hackerrank.com/contests/w26/challenges/pairs-again topic:

Given a number n, ask how many to a, a, a, a, a, xa+by=n xa+by=n have at least one solution, a<b A and 0<b,0<y 0, and X, Y, and X, s are integers. Problem Solving Process:

Game time, the original game luck is good, the problem saw a bunch of people WA more than 60, so did not dare to do, and then to fill up, but also to learn how to preprocess approximate. Topic Analysis:

The time limit of this problem is very strange, can run to 59 seconds.

All the approximate numbers of the first n are preprocessed here. Then enumerate A and x x, N−xa n-xa BX BX, the BX bx as b b, recording the BX BX more than a a, but for ax+by=cx+by=n ax+by = cx + by = n situation to go to a heavy.

The pretreatment of the approximate method can be compared with the idea of Euler sieve. AC Code:

 #include <bits/stdc++.h> using namespace std;

const int MAX = 312345;
Vector<int> Divisor[max];

int Flag[max];
    int main () {int n;
    CIN >> N; Approximate number of pre-preprocessing N, analogy Euler sieve method for (int i = 2; i < n; i++) {for (int j = i; J < n; j + = i) {divisor[j
        ].push_back (i);
    }} int ans = 0;
    memset (flag, 0, sizeof (flag));
            for (int a = 1, a < n; a++) {for (int xa = A; xa < n; xa + = a) {int yb = N-XA;
                for (int i = 0; i < divisor[yb].size (); i++) {int t = Divisor[yb][i];
                Let each t be used once for each a, and ensure T > a if (flag[t] = = A | | t <= a) continue;
                Flag[t] = A;
            ans++;
}}} cout << ans; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.