CSU 1325: a very hard problem

Source: Internet
Author: User
1325: A very hard problemtime limit: 3 sec memory limit: 160 MB
Submit: 203 solved: 53
[Submit] [Status] [web board] Description

CX old wet often gets hacked, much hacked, and I am also numb. As a result, he often heard someone say "Animals" with deep affection!

One day, CX experienced a sudden whim, which gave everyone a difficult problem and claimed that he could continue to attack him if he could answer the question accurately. Otherwise, he would have to fight back.

This problem is:

Two numbers p and q are given, and then two numbers A and B are given for each question. Please find them separately:

1. How many sequential number pairs (x, y) meet the requirements of 1 <= x <= A, 1 <= Y <= B, and gcd (x, y) it is an approximate number of P;

2. How many sequential number pairs (x, y) meet the requirements of 1 <= x <= A, 1 <= Y <= B, and gcd (x, y) is a multiple of P.

 

 

Input

 

There is only one set of test data.

The first row has two numbers: p and q. (1 <p <10 ^ 7, 1 <q <1000 .)

Next, there will be Q rows, with two numbers A and B in each row. (1 <a, B <10 ^ 7)

 

 

 

Output

 

Output A total of Q rows. There are two numbers in each row. Separated by spaces.

The two corresponding answers in the question description.

 

(X, y) = (2, 3) and (x, y) = (3, 2) are considered as two different sequential number pairs!

 

 

 

Sample Input
6 38 815 3213 77
Sample output
58 1423 10883 24
Hint

 

For 64-bit integer types, use LLD, or CIN, cout. T_t


Csu_scsi

This is the question of last year's competition. At that time, I felt it was difficult to implement it with Euler.

Later, I learned to use the Mobius Inversion, but it has timed out. Whether the block is used or time-out.

Question: omitted

Idea: For the second type, direct (A/P) * (B/P) is the answer. It is not hard to understand.

For the first case:

Set

G (p) represents each factor of enumeration P. Di is used to calculate GCD (x, y) = di (1 <= x <= A, 1 <= Y <= B).

It is the value required by the question.

If we enumerate the divisor of each P at this time, it will time out.

This sub-statement can be converted to another t = di * X, then the sub-statement can be converted:

In this case, we only need to pre-process the last part, and then we can solve this problem with SQRT (min (a, B) time.

How to pre-process this part?

First, create a table to obtain the U [].

In this formula, Tom (t) = sigma (U [di], T % di = 0 & Di is a factor of P );

Since P is unique, We can find its factor, and then use its factor to filter the array hxl [], which is Tom (t );

The last hxl [], the first n sums, which are done in parts.

 1 #include<iostream>  2 #include<stdio.h>  3 #include<cstring>  4 #include<cstdlib>  5 #include<math.h>  6 using namespace std;  7    8 typedef long long LL;  9 const int maxn = 1e7+1; 10 bool s[maxn]; 11 int prime[670000],len = 0; 12 int yz[10002],ylen; 13 int mu[maxn]; 14 int hxl[maxn]; 15 void  init() 16 { 17     memset(s,true,sizeof(s)); 18     mu[1] = 1; 19     for(int i=2;i<maxn;i++) 20     { 21         if(s[i] == true) 22         { 23             prime[++len]  = i; 24             mu[i] = -1; 25         } 26         for(int j=1;j<=len && ((long long)prime[j])*i<maxn;j++) 27         { 28             s[i*prime[j]] = false; 29             if(i%prime[j]!=0) 30                 mu[i*prime[j]] = -mu[i]; 31             else32             { 33                 mu[i*prime[j]] = 0; 34                 break; 35             } 36         } 37     } 38 } 39 void solve(int  p) 40 { 41     ylen = 0; 42     int k = (int)sqrt(p*1.0); 43     int tmp; 44     for(int i=1;i<=k;i++) 45     { 46         if(p%i==0) 47         { 48             yz[++ylen] = i; 49             tmp = p/i; 50             if(tmp!=i) yz[++ylen] = tmp; 51         } 52     } 53     for(int i=1;i<=ylen;i++) 54     { 55         for(int j=yz[i],k=1;j<maxn;j=j+yz[i],k++) 56             hxl[j]=hxl[j]+mu[k]; 57     } 58     for(int i=2;i<maxn;i++) hxl[i] = hxl[i]+hxl[i-1]; 59 } 60 int main() 61 { 62     init(); 63     int  p,q,A,B; 64     scanf("%d%d",&p,&q); 65     solve(p); 66     while(q--) 67     { 68         scanf("%d%d",&A,&B); 69         if(A>B) swap(A,B); 70         long long ans1 = 0,ans2 = 0; 71         for(int i=1,la = 0;i<=A; i=la+1) 72         { 73             la = min(A/(A/i),B/(B/i)); 74             ans1 = ans1+(long long)(hxl[la]-hxl[i-1])*(A/i)*(B/i); 75         } 76         ans2 = (A/p)*(B/p); 77         printf("%lld %lld\n",ans1,ans2); 78     } 79     return 0; 80 } 81   82 /************************************************************** 83     Problem: 1325 84     User: 987690183 85     Language: C++ 86     Result: Accepted 87     Time:1052 ms 88     Memory:92044 kb 89 ****************************************************************/

 

CSU 1325: a very hard problem

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.