B1257 [CQOI2007]余数之和 数学,分块

这个题想明白之后很好做,但是不好想。我根本没想出来,上网看了一下才知道怎么做。。。

这个题其实得数是一个等差数列,然后一点点求和就行了。

上次NOIP就是没看出来规律,这次又是,下次先打表找规律!!!

题干:

Description
给出正整数n和k,计算j(n, k)=k mod 1 + k mod 2 + k mod 3 + … + k mod n的值
其中k mod i表示k除以i的余数。
例如j(5, 3)=3 mod 1 + 3 mod 2 + 3 mod 3 + 3 mod 4 + 3 mod 5=0+1+0+3+3=7
Input
输入仅一行,包含两个整数n, k。
1<=n ,k<=10^9

Output

输出仅一行,即j(n, k)。
Sample Input
5 3
Sample Output
7

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
#define duke(i,a,n) for(int i = a;i <= n;i++)
#define lv(i,a,n) for(int i = a;i >= n;i--)
#define clean(a) memset(a,0,sizeof(a))
const int INF = 1 << 30;
typedef long long ll;
typedef double db;
template <class T>
void read(T &x)
{
    char c;
    bool op = 0;
    while(c = getchar(), c < '0' || c > '9')
        if(c == '-') op = 1;
    x = c - '0';
    while(c = getchar(), c >= '0' && c <= '9')
        x = x * 10 + c - '0';
    if(op) x = -x;
}
template <class T>
void write(T x)
{
    if(x < 0) putchar('-'), x = -x;
    if(x >= 10) write(x / 10);
    putchar('0' + x % 10);
}
ll n,k,l,r,ans = 0;
int main()
{
    read(n);read(k);
    for(int i = 1;i <= n;i = r + 1)
    {
        ll d = k / i;
        l = k / (d + 1) + 1;
        r = d ? k / d : n;
        r = r > n ? n : r;
        ans += (r - l + 1) * (k - d * l + k - d * r) / 2;
    }
    printf("%lld",ans);
    return 0;
}

 

posted @ 2018-08-25 20:54  DukeLv  阅读(216)  评论(0编辑  收藏  举报