转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud

How Many Sets III


Time Limit: 2 Seconds      Memory Limit: 65536 KB

Given a set S = {1, 2, ..., n}, your job is to count how many set T satisfies the following condition:

Input

There are multiple cases, each contains only one integer n ( 1 ≤ n ≤ 109 ) in one line, process to the end of file.

Output

For each case, output an integer in a single line: the total number of set T that meets the requirmentin the description above, for the answer may be too large, just output it mod 100000007.

Sample Input

2
3

Sample Output

1
4

看到这种输出只和一个数有关的,而且还是整数,想都不想,先暴力求出前几项,然后oeis大法,查到公式后

a(n) = sum { i=1..n-1, j=1..floor((n-1)/i) } (n - i*j)

发现这个公式只是n^2的,于是我们需要优化其中的步骤,首先,对于第二维,我们很容易搞掉,那么对于第一维,我们发现其中有一个(n-1)/i,那么其实有很多是对应的,于是我们只需要枚举1到sqrt(n-1)即可。即对于每一个i,在公差在(n-1)/(i+1) + 1到(n-1)/i这个范围内是可求的,另外注意求一下其相对的情况,看上去比较轻松,然而我这种数学渣还是推了半个多小时才推出来的

 /**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author xyiyy @https://github.com/xyiyy
*/ #include <iostream>
#include <fstream> //#####################
//Author:fraud
//Blog: http://www.cnblogs.com/fraud/
//#####################
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype> using namespace std;
typedef long long ll; //
// Created by xyiyy on 2015/8/5.
// #ifndef ICPC_INV_HPP
#define ICPC_INV_HPP
typedef long long ll; void extgcd(ll a, ll b, ll &d, ll &x, ll &y) {
if (!b) {
d = a;
x = ;
y = ;
}
else {
extgcd(b, a % b, d, y, x);
y -= x * (a / b);
}
} ll inv(ll a, ll mod) {
ll x, y, d;
extgcd(a, mod, d, x, y);
return d == ? (x % mod + mod) % mod : -;
} #endif //ICPC_INV_HPP const ll mod = ; class TaskJ {
public:
void solve(std::istream &in, std::ostream &out) {
ll n;
while (in >> n) {
ll ans = ;
ll m = n - ;
ll num = inv(, mod);
for (ll i = ; i * i <= m; i++) {
ll r = m / i;
ll l = m / (i + ) + ;
if (l > r)continue;
ans += (n * i % mod * (r - l + ) % mod -
(1LL + i) * i % mod * num % mod * (l + r) % mod * (r - l + ) % mod * num % mod) % mod + mod;
ans %= mod;
if (i != r)ans += (n * r % mod - i * r % mod * (1LL + r) % mod * num % mod) % mod + mod;
ans %= mod;
}
out << ans << endl;
}
}
}; int main() {
std::ios::sync_with_stdio(false);
std::cin.tie();
TaskJ solver;
std::istream &in(std::cin);
std::ostream &out(std::cout);
solver.solve(in, out);
return ;
}

ZOJ3558 How Many Sets III(公式题)的更多相关文章

  1. 华东交通大学2018年ACM“双基”程序设计竞赛 C. 公式题 (2) (矩阵快速幂)

    题目链接:公式题 (2) 比赛链接:华东交通大学2018年ACM"双基"程序设计竞赛 题目描述 令f(n)=2f(n-1)+3f(n-2)+n,f(1)=1,f(2)=2 令g(n ...

  2. HDU 4762 Cut the Cake (2013长春网络赛1004题,公式题)

    Cut the Cake Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  3. 牛客网 牛客小白月赛1 E.圆与三角形-公式题

    E.圆与三角形   链接:https://www.nowcoder.com/acm/contest/85/E来源:牛客网     这个题把公式推一下, 发现就是1+sinA*r,sinA最大为1,所以 ...

  4. codeforces 340C Tourist Problem(公式题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Tourist Problem Iahub is a big fan of tou ...

  5. 89. Gray Code(公式题)

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

  6. codeforces GYM 100971F 公式题或者三分

    F. Two Points time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  7. LightOJ 1248 Dice (III) (水题,期望DP)

    题意:给出一个n面的色子,问看到每个面的投掷次数期望是多少. 析:这个题很水啊,就是他解释样例解释的太...我鄙视他,,,,, dp[i] 表示 已经看到 i 面的期望是多少,然后两种选择一种是看到新 ...

  8. 计蒜客 30990.An Olympian Math Problem-数学公式题 (ACM-ICPC 2018 南京赛区网络预赛 A)

    A. An Olympian Math Problem 54.28% 1000ms 65536K   Alice, a student of grade 66, is thinking about a ...

  9. 【BZOJ1426】收集邮票 概率DP 论文题 推公式题

    链接: #include <stdio.h> int main() { puts("转载请注明出处[辗转山河弋流歌 by 空灰冰魂]谢谢"); puts("网 ...

随机推荐

  1. asp.net 解决IE11下 From身份验证失效问题

    指定如何将 Cookie 用于 Web 应用程序. <forms cookieless="UseCookies" name="test" loginUrl ...

  2. 【结构型】Flyweight模式

    享元模式的主要目的.意图是为对象的大量使用提供一种共享机制.该模式的思想重在复用.共享复用.像文字.列表中的格子等这类多数都是需要考虑复用技术,否则必将大量耗费内存空间而使资源以及性能等大量耗费.该模 ...

  3. Java接口和抽象类的实现方法

    一.java中的接口本质上是加约束的抽象类 //抽象类 public abstract class AExample { public abstract int add(int x,int y); p ...

  4. 使用django+mysql+scrapy制作的一个小说网站

    小说网站用的程序都是千篇一律的,jieqi + guanguang,无聊时间学习python+django,也做了一个小说网站,下面说一说做这个网站一些过程, 制作这种采集站,最要紧的是要有一个好的采 ...

  5. 下拉列表框Combo Box

    Combo Box/Combo Box Ex 组合窗口是由一个输入框和一个列表框组成.创建一个组合窗口可以使用成员函数: BOOL CListBox::Create( LPCTSTR lpszText ...

  6. Matlab 矩阵运算

    1.Syms 和sym的区别: syms是定义多个符号是符号变量的意思 sym只能定义一个符号变量,但可以具体到这个符号变量的内容 例:syms f z; %定义下x和y f=sym('a+b+c') ...

  7. CreateFile FileSeek FileRead 直接读取数据

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  8. Windows 中默认安装的.Net 版本

    Windows contains a version of .NET by default. Here's a listing of them. XP .NET v1.0 -- Service pac ...

  9. expect: spawn id exp4 not open

    spawn rsync -avH --delete /home/dwetl/bin dwetl@10.128.8.151:/home/dwetl/bin sending incremental fil ...

  10. HDU_2553——n皇后问题,作弊

    在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上.你的任务是,对于给定的N,求出有多少种合法的放置方法.   Inp ...