[CQOI2007] I summed number - block divisible

topic

Topic
subject to the effect:

Gives a positive integer \ (n-, K \) , seeking \ (\ Sigma_ {i = 1 } ^ {n} {k \ bmod i} \)

Code

At first glance only violence, in fact, a slightly modified it becomes a block number theory.
\ (\ Sigma_ {i = 1 } ^ {n} {k \ bmod i} = \ Sigma_ {i = 1} ^ {n} {\ lfloor {\ frac {k} {i}} \ rfloor} \ times i \)
and then divided by the average division ratio of more than just a block i, set on the line board.

#include <iostream>

using namespace std;
typedef long long ll;
ll n,k,ans;

int main() {
	cin>>n>>k;
	ans=k*n;
	ll r,len;
	for(int l=1;l<=n;l=r+1) {
		if(k/l==0) {
			r=n;
			continue;
		}
		r=min(n,k/(k/l)),len=r-l+1;
		ans-=(l+r)*len/2*(k/l);
	}
	cout<<ans;
	return 0;
}

Guess you like

Origin www.cnblogs.com/wyc06/p/12651299.html