CF616E Sum of Remainders(分块&取模)

Posted Harris-H

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF616E Sum of Remainders(分块&取模)相关的知识,希望对你有一定的参考价值。

CF616E Sum of Remainders(分块&取模)

余数求和加强了一下范围,注意要边算边取模。

因为 n , m ≤ 1 0 13 n,m\\le 10^{13} n,m1013,直接乘法会爆掉 l l ll ll,所以先对运算数取模,然后再对结果取模。不然会WA。

// Problem: CF616E Sum of Remainders
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF616E
// Memory Limit: 250 MB
// Time Limit: 2000 ms
// Date: 2021-07-02 11:03:34
// --------by Herio--------

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull; 
const int N=1e3+5,M=2e4+5,inf=0x3f3f3f3f;
const ll mod=1e9+7;
#define mst(a,b) memset(a,b,sizeof a)
#define PII pair<int,int>
#define fi first
#define se second
#define pb emplace_back
#define SZ(a) (int)a.size()
#define ios ios::sync_with_stdio(false),cin.tie(0) 
void Print(int *a,int n){
	for(int i=1;i<n;i++)
		printf("%d ",a[i]);
	printf("%d\\n",a[n]); 
}
int main(){
	ll n,m,s;
	scanf("%lld%lld",&n,&m);
	s=(n%mod)*(m%mod)%mod;
	for(ll l=1,r;l<=min(n,m);l=r+1){
		r=min(n/(n/l),m);
		ll x=l+r,y=(r-l+1);
		if(x&1) y>>=1;
		else x>>=1;
		s=(s-((n/l)%mod)*(x%mod)%mod*(y%mod)%mod)%mod;
	}
	s=(s%mod+mod)%mod;
	printf("%lld\\n",s);
	return 0;
}

以上是关于CF616E Sum of Remainders(分块&取模)的主要内容,如果未能解决你的问题,请参考以下文章

codeforces 616E. Sum of Remainders 数学

CF687B Remainders Game

[题解] CF85D Sum of Medians

cf Maximum Sum of Digits

CF1006C Three Parts of the Array

[ CF Contest ] Sum of Round Numbers 题解