"Bzoj" 3309:dzy Loves Math-mo-colorimetric inversion optimization

Source: Internet
Author: User

3309:dzy Loves MathDescription

For positive integer n, define the maximum power exponent of the mass factor of N for f (n). For example F (1960) =f (2^3 * 5^1 * 7^2) =3, F (10007) =1, f (1) = 0.
Given a positive integer, a, B, for Sigma (Sigma (GCD (I,J))) (I=1..A, j=1..b).

Input

The first line is a number T, which indicates the number of queries.
Next T line, two number per line, a, B, indicates a query.

Output

For each query, output a non-negative integer as the answer.

Sample Input4
7558588 9653114
6514903 4451211
7425644 1189442
6335198 4957
Sample Output35793453939901
14225956593420
4332838845846
15400094813HINT

"Data Size"

t<=10000

1<=a,b<=10^7

Reference post: POPOQQQ

Stolen by

I am mainly talking about why ( -1) The contribution of k+1 is only when all the power exponents are equal when the t factor decomposition;

In order to optimize the inner loop, we need to put a A, B, pretreatment, then it becomes the third step, the T-factor decomposition to T = P1^a1*p2^a2*...*pk^ak;

Möbius inversion formula is easy to know, d| t want to make MU[T/D]! = 0 only the coefficients of each factor are <= 1, then the total D is 2k (K is the number of T-factor);

<1> When a1 = ... = AK = a , for F[d] easy to know only when all take is A-1, only for A-1, then there is only one situation; contribution is (A-1) * ( -1) ^k = A * ( -1) ^k + ( -1) ^k;

F[D] = a need to be solved by combining number, (the number of combinations is C (k,i), where I is the number of A-1, i.e. i = 0,1,..., k-1), here i! = k; (i = k is f[d] = A-1),

The contribution at this time is a * (( -1) ^0*c (k,0) + ( -1) ^1*c (k,1) +...+ ( -1) ^ (k-1) *c (k,k-1)) =?

Look directly (1-1) ^k will find the missing A * ( -1^k*c (k,k)) = a * ( -1) ^k, so after the first case, need-( -1) ^k = ( -1) ^k+1

<2> when there is ai! = AJ, the maximum power goes to a or A-1 is f[d], so it is impossible to have a sub-case discussion, while the other goes to AJ | Aj-1 will cause the odd and even change and make the f[d] become 0;

So the conclusion is that only a1 = A2 = ... = AK = A, σf[d]*μ (t/d) is not 0, and f[d] is not required to calculate, to get only the odd parity of k = > ( -1) ^ (k+1)

The preprocessing time complexity is the same as the Sieve method O (n), then the data processing time of each group is O (sqrt (n)), which is the block acceleration;

Coding Details: It uses recursion to get g[]: The current value is 0 or 1, i.e. ( -1) ^ (k+1), followed by prefix and is for block acceleration;

The same recursion is also used in the A[i]: the power exponent of the minimum quality factor of I, the exponential product of the VAL[I]:I minimum mass factor (P[1]^a[i], which is to judge A[i] when the g[i] is obtained) [A[j/val[i]] That is, whether the power exponent of the smallest mass factor is equal to the second lowest power exponent. Note that this is a recursive relationship, that is, does not mean to compare the smallest two power exponent, which is equivalent to the Markov chain ~ ~ ~ (just see someone else's code understanding) ingenious Orz

#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<stdlib.h>using namespacestd;#defineRep0 (I,L,R) for (int i = (l); i < (R); i++)#defineREP1 (I,L,R) for (int i = (l); I <= (r); i++)#defineRep_0 (i,r,l) for (int i = (r); i > (l); i--)#defineRep_1 (i,r,l) for (int i = (r); I >= (l); i--)#defineINF 0x7ffffffftypedefLong Longll;template<typename t>voidRead1 (T &m) {T x=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}  while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} M= x*F;} Template<typename t>voidRead2 (T &a,t &b) {Read1 (a); Read1 (b);} Template<typename t>voidRead3 (T &a,t &b,t &c) {Read1 (a); Read1 (b); Read1 (c);}Const intN = 1e7+7;intVis[n],g[n],p[n],a[n],val[n];voidinit () {rep0 (I,2, N) {        if(Vis[i] = =0) {p[++p[0]] =i; G[i]=1; A[i]=1; Val[i]=i; }         for(intj =1;p [J]*i < n;j++) {vis[i*P[J]] =1; if(I%p[j] = =0) {A[i*P[J]] = a[i]+1;//the power exponent of the minimum factorization;VAL[I*P[J]] = val[i]*P[j]; intTMP = i/Val[i]; if(TMP = =1) G[i*p[j]] =1;//there's only one quality factor .                ElseG[i*P[J]] = (a[tmp] = = A[i*p[j]]?-g[tmp]:0);  Break; } A[p[j]*i] =1;//indicates the number of p[j] is 1;VAL[I*P[J]] =P[j]; G[P[J]*i] = (a[i] = =1?-g[i]:0); }} rep0 (I,1, N) g[i] + = g[i-1];}intMain () {init (); intn,m,t,i,j;    Read1 (T);  while(t--) {read2 (n,m); ll ans=0; if(N >m) swap (N,M);  for(i =1; I <= n;i = j +1) {J= Min (n/(n/i), m/(m/i)); Ans+ = 1ll* (G[j]-g[i-1]) * (n/i) * (m/i); } printf ("%lld\n", ans);//I64D is the result of WA, which runs the system differently. }    return 0;}

"Bzoj" 3309:dzy Loves Math-mo-colorimetric inversion optimization

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.