传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1257

最近刚做了一道莫比乌斯的题,需要用到这种方法。

应该让k / i相等的一连串k % i相加,举个例子:

100 / 34 = 2 ... 32

100 / 35 = 2 ... 30

100 / 36 = 2 ... 28

...

100 / 50 = 2 ... 0

可以观察到,商相同的余数数列是公差为商的相反数的等差数列,用求和公式就可以O(1)计算。

那么程序该怎么写呢?注意,如果当前的除数是i,那么[i, n / (n / i)]这个区间所有的数作为除数时,商都相同,那么之后就简单了。

#include <cstdio>
#include <algorithm> int n, k;
long long ans; int main(void) {
scanf("%d%d", &n, &k);
int last, lmt = std::min(n, k), d;
long long tem;
if (n > k) {
ans = (long long)k * (n - k);
}
for (int i = 1; i <= lmt; i = last + 1) {
d = k / i;
last = std::min(k / d, lmt);
tem = last - i + 1;
ans += tem * (k % i) - ((tem * (tem - 1)) >> 1) * d;
}
printf("%lld\n", ans);
return 0;
}

  

_bzoj1257 [CQOI2007]余数之和sum【小技巧】的更多相关文章

  1. BZOJ 1257: [CQOI2007]余数之和sum

    1257: [CQOI2007]余数之和sum Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 3769  Solved: 1734[Submit][St ...

  2. bzoj 1257: [CQOI2007]余数之和sum 数学 && 枚举

    1257: [CQOI2007]余数之和sum Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 1779  Solved: 823[Submit][Sta ...

  3. BZOJ 1257: [CQOI2007]余数之和sum( 数论 )

    n >= k 部分对答案的贡献为 k * (n - k) n < k 部分贡献为 ∑ (k - ⌊k / i⌋ * i)  = ∑  , ⌊k / i⌋ 相等的数是连续的一段, 此时这段连 ...

  4. 1257: [CQOI2007]余数之和sum

    1257: [CQOI2007]余数之和sum Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 2001  Solved: 928[Submit][Sta ...

  5. BZOJ 1257: [CQOI2007]余数之和sum【神奇的做法,思维题】

    1257: [CQOI2007]余数之和sum Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 4474  Solved: 2083[Submit][St ...

  6. bzoj千题计划173:bzoj1257: [CQOI2007]余数之和sum

    http://www.lydsy.com/JudgeOnline/problem.php?id=1257 k%i=k-int(k/i)*i 除法分块,对于相同的k/i用等差序列求和来做 #includ ...

  7. 【bzoj1257】[CQOI2007]余数之和sum

    [bzoj1257][CQOI2007]余数之和sum 2014年9月1日1,9161 Description 给出正整数n和k,计算j(n, k)=k mod 1 + k mod 2 + k mod ...

  8. BZOJ1257 [CQOI2007]余数之和sum

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  9. 1257: [CQOI2007]余数之和sum - BZOJ

    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, ...

随机推荐

  1. dsBlog_杂类

    C++,MFC的综合类的博客. 1. http://www.cnblogs.com/mfryf/category/354043.html

  2. 6. IO复用:select 和 poll

    select #include <sys/select.h> #include <sys/time.h> int select(int maxfdp1, fd_set *rea ...

  3. arm-linux交叉编译环境搭建

    1.解压交叉编译工具链包 tar jxvf EABI-4.3.3_EmbedSky_20100610.tar.bz2 (笔者将交叉编译工具链包放在”/home ”目录下) 解压后会生成两个目录: [r ...

  4. Delphi和C++的语法区别 (关于构造和析构)

    目录 Delphi永远没办法在栈上创建一个对象 Delphi的构造函数更象是个类方法(静态成员函数) Delphi的析构函数中可以调用纯虚方法 Delphi在构造对象时自动将成员变量清零 Delphi ...

  5. day 18 面向对象的 继承

    一.概念性知识 面向对象的三大特征:     继承,多态,封装 继承是创建新类的一种方法,在python中,新建的类可以继承一个或多个父类,父类称作基类或超类,新建的类又称作派生类或子类. 继承的好处 ...

  6. Kills all phantomjs instances, disregard of their origin python关闭进程

    Python/Linux quit() does not terminate PhantomJS process · Issue #767 · SeleniumHQ/selenium https:// ...

  7. 正则_action

    http://wiki.ubuntu.org.cn/index.php?title=Python%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E6%93% ...

  8. cassandra解析-log写

    db/Keyspace.java 中的 apply 函数 中的 commitLogPosition = CommitLog.instance.add(mutation); 这一句确认是写log的代码. ...

  9. skynet源码阅读<1>--lua与c的基本交互

    阅读skynet的lua-c交互部分代码时,可以看到如下处理: struct skynet_context * context = lua_touserdata(L, lua_upvalueindex ...

  10. 【Codeforces 947A】 Primal Sport

    [题目链接] 点击打开链接 [算法] 不难看出,x1的范围是[x2-P(x2)+1,x2],x0的范围是[x1-P(x1)+1,x1] 我们可以先做一遍线性筛,然后暴力就可以了 [代码] #inclu ...