uva12716 GCD XOR (table finding rule + sieve method)

Source: Internet
Author: User
Tags greatest common divisor

Test instructions: Input integer (1=<n<=30000000), how many pairs of integers (a, a, b) are satisfied: 1=<b=<a=<n, and GCD (A, b) =a^b. For example N=7, there are 4 pairs: (3,2), (5,4), (6,4), (7,6)


Problem Solving Ideas:

See after the topic has been looking for greatest common divisor and different or between the relationship, but found for a long time did not find. So the decisive table found the following rules

The number of gcd (A, b) =a^b satisfies the following law, either a=b-1, or there is a certain multiplier of the satisfied conditions that have been obtained as follows:


According to the above laws, we can try to all the possibilities, above only print a part, the entire program 2 seconds or so to complete the preprocessing

So for each qualifying (A,A-1), we use a similar sieve method to overlay the time complexity O (NLOGN)

Code:

#include <cstdio> #include <cstring> #define MAXN 30000001using namespace Std;int d[maxn];int main () {    int maxn=30000000;    for (int i=3;i<=maxn;i++)    {        if (1== (i^ (i-1))) {            d[i]++;            int j,k;            for (j=i<<1,k=2;j<=maxn;j+=i,k++) {                if (k== (j^ (j-k)))                    d[j]++;}}    }    for (int i=3;i<=maxn;i++)        d[i]+=d[i-1];    int T;    scanf ("%d", &t);    int n;    for (int t=1;t<=t;t++) {        scanf ("%d", &n);        printf ("Case%d:%d\n", T,d[n]);    }    return 0;}



Harvest:

1, many problems in mathematics can be played by the table to find the law;

2, for a large data situation, a small detail may be in time-out, so when the data is very large, every step as far as possible to achieve optimal

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

uva12716 GCD XOR (table finding rule + sieve method)

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.