Given a positive integer B, evaluate the maximum integer A. If a * (a + B) is satisfied, the number of complete integers is obtained.

Source: Internet
Author: User

Question Link

 

Given a positive integer B, evaluate the largest integer a. The value of a * (A + B) is the number of complete integers, 1 <= B <= 10 ^ 9

 

Solution:

If we set the maximum common divisor of A and B to G, the maximum common divisor of A and A + B is also G, because the maximum common divisor has the property: gcd (A, B) = gcd (A, A + B)

In this way, we can further simplify a * (a + B) = G ^ 2 * A1 * (A1 + B1), where A1 and B1 must be mutually unique, because the maximum number of public approx. G has been removed, the interaction between A1 and A1 + B1 is also obtained.

Because the number of partitions is full, you can set x ^ 2 = A1, y ^ 2 = A1 + B1 and then release: b1 = y ^ 2-x ^ 2 = (Y-x) * (Y + x)

Now we make n = Y + X, M = Y-x obvious n> M. Because A1 and A1 + B1 are mutually unique, gcd (x ^ 2, y ^ 2) is available) and then gcd (x, y) = 1.

 

Here we can do this:

First, obtain all the dikes of B, so that G is equal to the divisor of B/B, and then calculate the divisor of B respectively. Assume that it is arr2 [J], because n> m, we only need to enumerate SQRT (arr1 [I]), and then solve

Output x = (n-M)/2,Y = (n + M)/2, but the premise is to ensure that all can be divided, and then judge gcd (x, y) = 1, two-layer for loop, record the maximum value. As for how to calculate a number factor, the previousArticleAlready

Given, it is the prime factor decomposition.In addition to DFS, there are usually not many factors, so there is generally no problem. Do not view meCode, Messy.

# Include <iostream> # include <string. h> # include <algorithm> # include <stdio. h> using namespace STD; typedef long ll; const int n = 1000010; const int M = 1050; bool prime [N]; ll p [N]; ll PR1 [m]; ll kk1 [m]; ll Pr2 [m]; ll kk2 [m]; ll K = 0; ll C1, R1; ll arr1 [m]; ll C2, R2; ll arr2 [m]; void isprime () {ll I, j; memset (Prime, true, sizeof (PRIME); for (I = 2; I <n; I ++) {If (prime [I]) {P [k ++] = I; for (j = I + I; j <N; J + = I) {Prime [J] = false ;}}} Void calfactor1 (ll n) {ll t = n, I, A; C1 = 0; for (I = 0; P [I] * P [I] <= N; I ++) {A = 0; If (N % P [I] = 0) {PR1 [C1] = P [I]; while (N % P [I] = 0) {A ++; N/= P [I];} kk1 [C1] = A; C1 ++ ;}} if (n> 1) {PR1 [C1] = N; kk1 [C1] = 1; C1 ++ ;}} void dfs1 (ll dep, ll product) {If (DEP = C1) {arr1 [R1 ++] = product; return ;}for (ll I = 0; I <= kk1 [Dep]; ++ I) {dfs1 (DEP + 1, product); product * = PR1 [Dep] ;}} void calfactor2 (LL N) {ll t = N, I, A; C2 = 0; for (I = 0; P [I] * P [I] <= N; I ++) {A = 0; if (N % P [I] = 0) {Pr2 [C2] = P [I]; while (N % P [I] = 0) {A ++; n/= P [I];} kk2 [C2] = A; C2 ++ ;}} if (n> 1) {Pr2 [C2] = N; kk2 [C2] = 1; C2 ++ ;}} void dfs2 (ll dep, ll product) {If (DEP = c2) {arr2 [R2 ++] = product; return;} For (ll I = 0; I <= kk2 [Dep]; ++ I) {dfs2 (DEP + 1, product ); product * = Pr2 [Dep];} ll gcd (ll a, LL B) {return B? Gcd (B, A % B): A;} int main () {ll m, n, I, j, G, B, Val, T; isprime (); cin> T; while (t --) {CIN> B; R1 = R2 = 0; calfactor1 (B); dfs1 (0, 1); sort (arr1, arr1 + R1); LL max = 0; for (I = 0; I <R1; I ++) {r2 = 0; G = B/arr1 [I]; memset (arr2, 0, sizeof (arr2); calfactor2 (arr1 [I]); dfs2 (0, 1); For (j = 0; j <R2; j ++) {If (arr2 [J] * arr2 [J] <arr1 [I]) {n = arr1 [I]/arr2 [J]; M = arr2 [J]; if (n-m) % 2 = 0 & (n + M) % 2 = 0 & gcd (n + M)/2, (N-M)/2) = 1) {val = g * (n-M)/2) * (n-M)/2 ); if (Val> MAX) max = Val ;}}} cout <max <Endl;} 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.