satisfactory pairs hackerrank-pairs-again--pretreatment + indefinite array application __ Elementary Number Theory

Source: Internet
Author: User

Do you:
1: Input n, ask (A, b) to meet a < b and exist x, y so that x*a + y*b = N of the two-tuple number, require N, a, b, X, y are all positive integers
2 method: First preprocessing less than N of the number of divisors, and then the violent temptation + pruning, the test of the number of two groups, the time complexity 1/x from 1 to n integral, for NLN (n)

Vjudge Topic link

Recommended Reference Blog 1
Recommended Reference Blog 2

The following is the accepted code

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

const int N = 3e5 + 4;
Vector <int> Y_b[n];

Vector <int>:: iterator B;

int book[n];
    int main () {int n, I, J, a, X, T, ans;
    scanf ("%d", &n); for (i = 1; I <= n; i++) {for (j = 1; j*j <= i; j +) {if (i%j = 0) {Y_b[i].push_ba
                CK (j);
                    if (I*i!= j) {t = i/j;
                Y_b[i].push_back (t);
    Sort (Y_b[i].begin (), Y_b[i].end (), greater<int> ());
    Ans = 0;
            for (a = 1; a < n; a++) {for (x = 1; x*a < n x + +) {t = n-x*a;
                for (b = Y_b[t].begin (), B!= y_b[t].end (); b++) {if ((*b) <= a) break;
                    if (Book[*b]!= a) {ans++;
                BOOK[*B] = A;
    }} printf ("%d\n", ans);
return 0;
 }

The following is the recommended reference code

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

const int N = 3e5 + 4;

Vector <int> y_b[n];/* Records the divisors of the current element/vector <int>:: iterator B;

int book[n];/* real-type global variable initialization defaults to 0*/BOOL CMP (int a, int b) {return a > B;}
    int main () {int n, I, J, a, X, T, ans;
    scanf ("%d", &n); for (i = 1; I <= n; i++) {for (j = 1; j*j <= i; j +) {if (i%j = 0) {Y_b[i].push_ba
                CK (j);
                    if (I*i!= j) {t = i/j;
                Y_b[i].push_back (t);
    Sort (Y_b[i].begin (), Y_b[i].end (), greater<int> ());
    Ans = 0;
            for (a = 1; a < n; a++) {for (x = 1; x*a < n x + +) {t = n-x*a; for (b = Y_b[t].begin (); b!= Y_b[t].end (); b++) {if (*b) <= a) *: A < b*/break
                ;
                    if (Book[*b]!= a) {/* Mark heavy */ans++;
    BOOK[*B] = A;            }} printf ("%d\n", ans);
return 0;
 }

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.