BZOJ 2956: 模积和

题目

2956: 模积和

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 554  Solved: 257
[Submit][Status]

Description

 求∑∑((n mod i)*(m mod j))其中1<=i<=n,1<=j<=m,i≠j。

  

Input

第一行两个数n,m。

Output

  一个整数表示答案mod 19940417的值

Sample Input

3 4

Sample Output

1

样例说明
  答案为(3 mod 1)*(4 mod 2)+(3 mod 1) * (4 mod 3)+(3 mod 1) * (4 mod 4) + (3 mod 2) * (4 mod 1) + (3 mod 2) * (4 mod 3) + (3 mod 2) * (4 mod 4) + (3 mod 3) * (4 mod 1) + (3 mod 3) * (4 mod 2) + (3 mod 3) * (4 mod 4) = 1

数据规模和约定
  对于100%的数据n,m<=10^9。

题解

这道题目我调了好久,有两点错:一是乱用除法QAQ,要乘逆元才对!二是错误的估计了结果的大小,少用了几个%。虽然取模的数很小,但是两个乘在一起照样让你爆精度!每步都要取模!

这题的思路是分别计算∑∑((n mod i)*(m mod j))和∑∑((n mod i)*(m mod j))[i==j]的值,并且作差。

最后化简得式子是

∑∑((n mod i) * (m mod j)) 1<=i<=n, 1<=j<=m, i≠j=

∑(n mod i) * ∑(m mod i) - ∑((n mod i) * (m mod i))=

∑(n-[n/i]*i) * ∑(m-[m/i]*i) - ∑(nm-([n/i]+[m/i])i+[n/i][m/i]*i*i)【右式中[]表示下取整】

代码

 1 /*Author:WNJXYK*/
 2 #include<cstdio>
 3 #include<iostream>
 4 using namespace std;
 5 const long long M=19940417;
 6
 7 long long n,m;
 8
 9
10 inline int remin(int a,int b){
11     if (a<b) return a;
12     return b;
13 }
14
15 inline long long sum(long long n){
16     return n*(n+1)%M*(2*n+1)%M*3323403%M;
17 }
18
19 inline long long getDist(int x,int y){
20     return (x+y)%M*(y-x+1)%M*9970209%M;
21 }
22
23 inline long long getOneAns(long long x){
24     long long ans=((long long)x*(long long)x)%M;
25     long long pos;
26     for (long long i=1;i<=x;i=pos+1){
27         pos=remin(x/(x/i),x);
28         //cout<<"+"<<(long long)x*(long long)(pos-i+1)%M<<endl;
29         //cout<<"-"<<((long long)((long long)(pos)*(long long)(pos+1)/(long long)2-(long long)(i-1)*(long long)(i)/(long long)2)*(long long)(x/i))%M<<endl;
30         //cout<<"x"<<(long long)(pos)*(long long)(pos+1)/(long long)2-(long long)(i-1)*(long long)(i)/(long long)2<<endl;
31         //ans=ans-((long long)getDist(i,pos)%M*(long long)(x/i))%M;
32         ans-=(x/i)*getDist(i,pos)%M;
33         while(ans<0) ans+=M;
34     }
35     //cout<<endl;
36     while(ans<0) ans+=M;
37     return ans;
38 }
39
40 inline long long getTwoAns(long long x1,long long x2){
41     long long ans=0;
42     long long pos;
43     long long x=remin(x1,x2);
44     ans=x%M*x1%M*x2%M;
45     for (long long i=1;i<=x;i=pos+1){
46         pos=remin(x1/(x1/i),x2/(x2/i));
47         //cout<<"POS"<<i<<"~"<<pos<<endl;
48         //cout<<"+"<<(long long)(pos-i+1)*(long long)x1*(long long)x2<<endl;
49         //cout<<"-"<<(long long)x1*(long long)(x2/i)*getDist(i,pos)<<endl;
50         ///cout<<"-"<<(long long)x2*(long long)(x1/i)*getDist(i,pos)<<endl;
51         //cout<<"+"<<((long long)((long long)x1/(long long)i)*(long long)((long long)x2/(long long)i))*getSDist(i,pos)<<endl;
52         //cout<<"————————————"<<endl;
53         ans=ans-(long long)x1%M*(long long)(x2/i)%M*getDist(i,pos)%M-(long long)x2%M*(long long)(x1/i)%M*getDist(i,pos)%M+((long long)((long long)x1/(long long)i)%M*(long long)((long long)x2/(long long)i))%M*(sum(pos)-sum(i-1))%M;
54         while(ans<0) ans+=M;
55     }
56     while(ans<0) ans+=M;
57     return ans;
58 }
59
60 int main(){
61     scanf("%lld%lld",&n,&m);
62     long long Ans=((long long)getOneAns(n)*(long long)getOneAns(m))%M;
63     Ans-=getTwoAns(n,m);
64     //cout<<getOneAns(n)<<endl;
65     //cout<<getOneAns(m)<<endl;
66     //cout<<getTwoAns(n,m)<<endl;
67     while(Ans<0) Ans+=M;
68     printf("%lld\n",Ans%M);
69     return 0;
70 }

时间: 2024-12-19 04:05:43

BZOJ 2956: 模积和的相关文章

BZOJ 2956 模积和(分块)

[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2956 [题目大意] 求∑∑((n%i)*(m%j))其中1<=i<=n,1<=j<=m,i≠j. [题解] $∑_{i=1}^{n}∑_{j=1}^{m}((n\mod i)*(m\mod j))(i≠j)$ $=∑_{i=1}^{n}∑_{j=1}^{m}(n-\lfloor n/i\rfloor*i)*(m-\lfloor m/j\rfloor*j)-∑_{i=1}

[Bzoj 2956] 模积和 (整除分块)

整除分块 一般形式:\(\sum_{i = 1}^n \lfloor \frac{n}{i} \rfloor * f(i)\). 需要一种高效求得函数 \(f(i)\) 的前缀和的方法,比如等差等比数列求和或对于积性函数的筛法等,然后就可以用整除分块的思想做. 题目解法 化公式变成比较方便的形式: \(\ \sum_{i = 1}^n \sum_{j = 1}^m (n \mod i)(m \mod j), i \ne j\) \(= \sum_{i = 1}^n \sum_{j = 1}^m

【BZOJ2956】模积和 分块

[BZOJ2956]模积和 Description 求∑∑((n mod i)*(m mod j))其中1<=i<=n,1<=j<=m,i≠j. Input 第一行两个数n,m. Output 一个整数表示答案mod 19940417的值 Sample Input 3 4 Sample Output 1 样例说明 答案为(3 mod 1)*(4 mod 2)+(3 mod 1) * (4 mod 3)+(3 mod 1) * (4 mod 4) + (3 mod 2) * (4 m

模积和(bzoj 2956)

求∑∑((n mod i)*(m mod j))其中1<=i<=n,1<=j<=m,i≠j. 对于100%的数据n,m<=10^9. 对于而言,他的个数很小,最大是63244,因此可以枚举分块确定区域 [ l , n/(n/l) ] c++ code: #include <bits/stdc++.h> using namespace std; const int mod=19940417; const int ni=3323403; typedef long lo

【bzoj2956】模积和 数论+分块

题目描述 求∑∑((n mod i)*(m mod j))其中1<=i<=n,1<=j<=m,i≠j. 输入 第一行两个数n,m. 输出 一个整数表示答案mod 19940417的值 样例输入 3 4 样例输出 1 题解 数论+分块 由于直接求i≠j的情况比较难搞,所以我们可以先求出i可以等于j的和,然后再减去i等于j时的情况. 也就是求∑∑((n mod i)*(m mod j))-∑((n mod i)*(m mod i)). 然后再根据乘法分配律转化为∑(n mod i)*∑

P2260 [清华集训2012]模积和

题目背景 数学题,无背景. 题目描述 求 $\sum\limits_{i=1}^{n} \sum\limits_{j=1}^{m} (n \bmod i) \times (m \bmod j), i \neq j \;\bmod\;19940417$ 的值 输入输出格式 输入格式: 两个整数n m 输出格式: 答案 mod 19940417 输入输出样例 输入样例#1: 3 4 输出样例#1: 1 输入样例#2: 123456 654321 输出样例#2: 116430 说明 30%: n,m

bzoj2956 模积和

Description 求∑∑((n mod i)*(m mod j))其中1<=i<=n,1<=j<=m,i≠j. Input 第一行两个数n,m. Output 一个整数表示答案mod 19940417的值 Sample Input 3 4 Sample Output 1 样例说明答案为(3 mod 1)*(4 mod 2)+(3 mod 1) * (4 mod 3)+(3 mod 1) * (4 mod 4) + (3 mod 2) * (4 mod 1) + (3 mod

ACM学习历程—BZOJ2956 模积和(数论)

Description 求∑∑((n mod i)*(m mod j))其中1<=i<=n,1<=j<=m,i≠j. Input 第一行两个数n,m. Output 一个整数表示答案mod 19940417的值 Sample Input 3 4 Sample Output 1 样例说明 答案为(3 mod 1)*(4 mod 2)+(3 mod 1) * (4 mod 3)+(3 mod 1) * (4 mod 4) + (3 mod 2) * (4 mod 1) + (3 mod

[清华集训2012](洛谷2260)模积和

传送门:here 一句话题意:求$ \sum\limits_{i=1}^n\sum\limits_{j=1}^m(n\ mod\ i)(m\ mod\ j)$,$ i \neq j$ 简化题意:                                                                                           假设$ n<m$ 我们令$ f(x)$表示$ \sum\limits_{i=1}^x\ x\ mod\ i$,$ f2(x,