Bzoj 1101 ([poi2007]zap-satisfies the number of X<=A&&Y<=B&&GCD (x, y) =d)

Source: Internet
Author: User

1101: [Poi2007]zap time limit: ten Sec Memory Limit: 162 MB
Submit: 1646 Solved: 577
[Submit] [Status] [Discuss] Description

FGD is cracking a cipher, and he needs to answer a number of similar questions: for a given integer, a, B, and D, how many positive integers are to X, Y, X<=a,y<=b, and gcd (x, y) =d. As a classmate of FGD, FGD hopes to get your help.

Input

The first line contains a positive integer n, which indicates that there are altogether n groups of queries. (1<=n<= 50000) next n rows, each line represents a query, three positive integers per line, respectively, A,b,d. (1<=d<=a,b<=50000)

Output

For each set of queries, output to the output file zap.out a positive integer that represents the integer logarithm that satisfies the condition.

Sample Input2
4 5 2
6 4 3Sample Output3
2HINT

For the first set of queries, an integer pair that satisfies the condition has (2,2), (2,4), (4,2). For the second set of queries, an integer pair that satisfies the condition has (6,3), (3,3).

Source



This is entitled Mobius Inversion:

PS: The calculation process cannot use long long or tle

Formula derivation

Let's take a look at popoqqq ppt "Möbius inversion"

















#include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include < functional> #include <iostream> #include <cmath>using namespace std; #define for (i,n) for (int i=1;i<=n ; i++) #define Fork (i,k,n) for (int i=k;i<=n;i++) #define MEM (a) memset (A,0,sizeof (a)); #define MAXN (50000+10) typedef Long Long ll;int p[maxn]={0},tot;bool b[maxn]={0};int mu[maxn]={0},sum[maxn]={0};inline int read () {int X=0,f=1;char ch= GetChar (); while (ch< ' 0 ' &&ch> ' 9 ') {if (ch== '-') f=-1; Ch=getchar ();} while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} void Make_prime (int n) {tot=0; mu[1]=1; Fork (i,2,n) {if (!b[i]) p[++tot]=i,mu[i]=-1; for (J,tot) {if (i*p[j]>n) break;b[i*p[j]]=1;if (i%p[j]==0) {mu[i*p[j]]=0;  Mu[i*p[j]]=-mu[i]; }}sum[0]=0; for (I,n) sum[i]=sum[i-1]+mu[i];} int N,m,d;int calc () {int ans=0;for (int i=1,last=1;i<=n;i=last+1) {last=min (n/(n/i), m/(m/i)), ans+= (sum[last]-sum[ I-1]) * (n/i) * (m/i);} printf ("%d\n", aNS); return ans;} int main () {//freopen ("bzoj1101.in", "R", stdin); MEM (P) mem (b) Mem (MU) mem (sum) int N = 50000;make_prime (n); int T;  T=read (); while (t--) {n=read (); M=read (); D=read (); if (n>m) swap (n,m); N/=d,m/=d;calc ();} return 0;}





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

Bzoj 1101 ([poi2007]zap-satisfies the number of X<=A&&Y<=B&&GCD (x, y) =d)

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.