ACM Learning process-bzoj2956 modulus and (number theory)

Source: Internet
Author: User
Tags modulus

Description

Ask for ∑∑ ((n mod i) * (M mod j)) where 1<=i<=n,1<=j<=m,i≠j.

  

Input

The first line is two numbers n,m.

Output

An integer representing the value of the answer mod 19940417

Sample Input


3 4

Sample Output

1

Sample Description
The answer is (3 mod 1) * (4 mod 2) + (3 mod 1) * (4 mod 3) + (3 mod1) * (4 mod 4) + (3 mod 2) * (4 mod 1) + (3 mod 2) * (4 mod 3) + (3 mod 2) * (4mod 4) + (3 mod 3) * (4 mod 1) + (3 mod 3) * (4 mod 2) + (3 mod 3) * (4 mod 4) = 1

Data size and conventions
For data n,m<=10^9 of 100%.

By test instructions:

∑∑ ((n mod i) * (Mmod J)) (I≠j) =∑ (n mod i) *∑ (M mod i)-∑ ((n mod i) * (M mod i)) =∑ (n-[n/i]*i) *∑ (m-[m/i]*i)-∑ ([n /I]+[M/I]) i+[n/i][m/i]*i*i) =∑ (n-[n/i]*i) *∑ (m-[m/i]*i) –n*n*m+∑[n/i]i+∑[m/i]i-∑[n/i][m/i]*i*i (n <= m)

then use [n/i] can be accelerated, but there are a number of intermediate processes that need to be noted,

m/(m/i) the time needed and N Compare size because it may be out of range.

It is also int multiplication may explode, need to turn Long Long , the middle process, don't forget. MOD .

Code:

#include <iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<cstring>#include<algorithm>#include<Set>#include<map>#include<queue>#include<string>#defineLL Long Long#defineMOD 19940417#defineNsix 3323403using namespacestd;intN, M; LL Cal (intLenintx) {LL ans=0, TMP; intJ;  for(inti =1; I <= Len; ++i) {j= min (len, x/(x/i));//This sentence doesn't have to be min,j to cross .TMP = ((LL) i+j) * (j-i+1)/2%MOD; Ans+ = tmp* (x/i)%MOD; Ans%=MOD; I=J; }    returnans;} inline ll sum (ll x) {returnx* (x+1)%mod* (2*x+1)%mod*nsix%MOD;} LL Cal2 (intXinty) {LL ans=0, TMP, ttmp; intJ;  for(inti =1; I <= x; ++i) {j= Min (x/(x/i), y/(y/i)); //j = min (j, x);TMP = SUM (j)-sum (I-1); TMP= (tmp%mod+mod)%MOD; Ttmp= (LL) x/i) * (y/i)%MOD; Ans+ = tmp*ttmp%MOD; Ans%=MOD; I=J; }    returnans;}voidWork () {if(N >m) Swap (n, m);    LL ans, m2, N2, SNN, SMM, SNM, SS; M2= (LL) m*m%MOD; N2= (LL) n*n%MOD; SMM=Cal (M, m); SNN=Cal (n, N); SNM=Cal (n, m); SS=cal2 (n, m); Ans= M2*n2%mod-m2*snn%mod-n2*smm%mod + snn*smm%MOD; Ans-= m*n2%MOD; Ans+ = m*snn%MOD; Ans+ = n*snm%MOD; Ans-=SS; Ans= (ans%mod+mod)%MOD; printf ("%lld\n", ans);}intMain () {//freopen ("test.in", "R", stdin);     while(SCANF ("%d%d", &n, &m)! =EOF) work (); return 0;}

ACM Learning process-bzoj2956 modulus and (number theory)

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.