Acdream 1148 GCD sum Mobius Inversion ansx, ansy

Source: Internet
Author: User
GCD sumtime limit: 8000/4000 ms (Java/others) memory limit: 128000/64000 KB (Java/others) submitstatisticnext problemproblem description gives n, m
Run the following program:
Long long ans = 0, ansx = 0, ansy = 0;
For (INT I = 1; I <= N; I ++)
For (Int J = 1; j <= m; j ++)
If (gcd (I, j) = 1) ans ++, ansx + = I, ansy + = J;
Cout <ans <"" <ansx <"" <ansy <Endl; Input

Multiple groups of data, with N and M (1 <= n, m <= 100000) in each row ).

 

Output is as described in the question. Each row outputs three numbers, which are separated by spaces: ANS, ansx, and ansy.
5 51 3
Sample output
19 55 553 3 6
The total hint data is less than 50000. It is easier for the first digit to use Mobius for inversion. For ansx, it is similar to ansy. Now we will discuss ansx practices. let's set F (d) To represent the sum of X when 1 <= x <= N, 1 <= Y <= m, gcd (x, y) = D. when we set F (d) to represent 1 <= x <= N, 1 <= Y <= m, and gcd (x, y) to a multiple of D, so F (1) = F (1) + F (2) + f (3 )...... F (min (n, m) F (2) = F (2) + f (4) + f (6 ).... F (min (n/2, M/2); f (I) = f (I) + f (I * 2) + f (I * 3 ).... F (min (N/I, m/I); therefore, we can find that due to d = 1, we can filter Mu [I] * I based on the inversion, calculate the sum of the First n items. Use parts to complete the process. Code:
 1 #include<iostream> 2 #include<stdio.h> 3 #include<cstring> 4 #include<cstdlib> 5 using namespace std; 6 typedef long long LL; 7  8 const int maxn = 1e5+3; 9 bool s[maxn];10 int prime[maxn],len = 0;11 int mu[maxn];12 LL hxl [maxn];13 int sum1[maxn];14 void  init()15 {16     memset(s,true,sizeof(s));17     mu[1] = 1;18     for(int i=2;i<maxn;i++)19     {20         if(s[i] == true)21         {22             prime[++len]  = i;23             mu[i] = -1;24         }25         for(int j=1;j<=len && (long long)prime[j]*i<maxn;j++)26         {27             s[i*prime[j]] = false;28             if(i%prime[j]!=0)29                 mu[i*prime[j]] = -mu[i];30             else31             {32                 mu[i*prime[j]] = 0;33                 break;34             }35         }36     }37     for(int i=1;i<maxn;i++)38         sum1[i] = sum1[i-1]+mu[i];39     hxl[1] = mu[1];40     for(int i=2;i<maxn;i++){41         hxl[i] = i*mu[i]+hxl[i-1];42     }43 }44 int main()45 {46     init();47     int n,m;48     while(scanf("%d%d",&n,&m)>0)49     {50         LL sum = 0;51         LL ansi = 0,ansj = 0;52         int a = n;53         int b = m;54         if(a>b) swap(a,b);55         for(int i=1,la = 0;i<=a;i++,i = la+1)56         {57             la = min(a/(a/i),b/(b/i));58             sum = sum + ((LL)(a/i))*(b/i)*(sum1[la]-sum1[i-1]);59             ansi = ansi +(hxl[la]-hxl[i-1])*(((LL)(n/i+1)*(n/i))/2)*(m/i);60             ansj = ansj +(hxl[la]-hxl[i-1])*(((LL)(m/i+1)*(m/i))/2)*(n/i);61         }62         printf("%lld %lld %lld\n",sum,ansi,ansj);63     }64     return 0;65 }

 

Acdream 1148 GCD sum Mobius Inversion ansx, ansy

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.