2956: 模积和
2019-04-13 14:25 发布
生成海报
2956: 模积和
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1284 Solved: 580
[Submit ][Status ][Discuss ]
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。
HINT
Source
中国国家队清华集训
2012-2013 第一天
[Submit ][Status ][Discuss ]
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long LL;
const LL mo = 19940417;
int n,m,Inv2,Inv6;
int Mul(const LL &x,const LL &y) {return x * y % mo;}
int Add(const int &x,const int &y) {return (x + y) % mo;}
int Dec(const int &x,const int &y) {return (x - y + mo) % mo;}
int sum(int N)
{
int G = 1 + N;
if (G & 1) N >>= 1; else G >>= 1;
return Mul(N,G);
}
int sum2(int N)
{
int F = N + 1,G = 2 * N + 1;
bool f2,f3; f2 = f3 = 0;
if (!(N & 1) && !f2) f2 = 1,N >>= 1;
if (!(F & 1) && !f2) f2 = 1,F >>= 1;
if (!(G & 1) && !f2) f2 = 1,G >>= 1;
if (N % 3 == 0 && !f3) f3 = 1,N /= 3;
if (F % 3 == 0 && !f3) f3 = 1,F /= 3;
if (G % 3 == 0 && !f3) f3 = 1,G /= 3;
return Mul(N,Mul(F,G));
}
int Work(int N)
{
int ret = 0;
for (int i = 1,last; i <= N; i = last + 1)
{
int x = N / i; last = N / x;
ret = Add(ret,Mul(last - i + 1,N));
ret = Dec(ret,Mul(x,Dec(sum(last),sum(i-1))));
}
return ret;
}
int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#endif
cin >> n >> m;
int Ans = Mul(Work(n),Work(m));
for (int i = 1,last; i <= min(n,m); i = last + 1)
{
int x = n / i,y = m / i,tot;
last = min(n / x,m / y);
tot = Mul(n,Mul(m,last - i + 1));
tot = Dec(tot,Mul(n,Mul(y,Dec(sum(last),sum(i-1)))));
tot = Dec(tot,Mul(m,Mul(x,Dec(sum(last),sum(i-1)))));
tot = Add(tot,Mul(x,Mul(y,Dec(sum2(last),sum2(i-1)))));
Ans = Dec(Ans,tot);
}
cout << Ans << endl;
return 0;
}
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮