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)