Bzoj 1101: [Poi2007]zap

Source: Internet
Author: User
Tags gcd

1101: [Poi2007]zap time limit:10 Sec Memory limit:162 MB
submit:2262 solved:895
[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 to X, Y, meet 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 lines, each line represents one query, three per line
positive integers, respectively, for 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
2
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). HINT Source Analysis:

In fact, the equivalent of seeking n/d,m/d limit, gcd (i,j) ==1 the number of IJ

The same practice as http://www.cnblogs.com/neighthorn/p/6214769.html ...

Note that the question O (n) is definitely not going to be ... Without looking at the data range, I'm going to go to the tle.

Because (n/i) * (m/i) The value of up to sqrt (n) +sqrt (m), so we pre-processing μ prefix and, segmentation calculation is good ...

Code:
1#include <algorithm>2#include <iostream>3#include <cstring>4#include <cstdio>5 //by Neighthorn6 using namespacestd;7 //Dapeng Day with the wind, soaring 90,000 miles8 9 Const intmaxn=50000+5;Ten  One intN,M,D,CAS,CNT,MIU[MAXN],VIS[MAXN],PRIME[MAXN]; A  - Long Longans=0; -  theSigned Main (void){ -memset (Vis,0,sizeof(VIS)); Cnt=0; miu[1]=1; -      for(intI=2; i<=50000; i++){ -         if(!Vis[i]) +prime[++cnt]=i,vis[i]=1, miu[i]=-1; -          for(intj=1; j<=cnt&&prime[j]*i<=50000; j + +){ +vis[i*prime[j]]=1; A             if(i%prime[j]==0){ atmiu[i*prime[j]]=0; Break; -             } -miu[i*prime[j]]=-Miu[i]; -         } -     } -      for(intI=1; i<=50000; i++) inmiu[i]+=miu[i-1]; -scanf"%d",&CAs); to      while(cas--){ +scanf"%d%d%d",&n,&m,&d); -n/=d,m/=d;ans=0; the         if(n>m) * swap (n,m); $          for(intI=1, r;i<=n;i=r+1){Panax NotoginsengR=min (n/(n/i), m/(m/i)); -ans+= (Long Long) (miu[r]-miu[i-1]) * (n/i) * (m/i); the         } +printf"%lld\n", ans); A     } the     return 0; +}
View Code

by Neighthorn

Bzoj 1101: [Poi2007]zap

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.