codeforces 616E - Sum of Remainders
2016-01-15 09:28
387 查看
题意:计算 n mod 1+n mod 2 + ... n mod m
思路:其实计算 n mod x 可以转换为 n - [n/x]*x n/x=i 是一个连续的范围 比如 10/4 =2 10/5=2 我们只要计算出 n/x=i 的一个范围 ,就可以用等差公式求和了
ps :上网查了一下 有开方的策略 好像才是正确姿势 = = 代码太丑 爆了好多次LL
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<stdio.h>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define N 100010
#define MOD 1000000007
#define LL long long
LL n,m,ans;
LL divide2(LL x,LL y)
{
if(x%2==0)
return (((x/2)%MOD)*(y%MOD))%MOD;
else
return ((x%MOD)*((y/2)%MOD))%MOD;
}
int main()
{
scanf("%lld%lld",&n,&m);
ans=((max(0LL,m-n)%MOD)*(n%MOD)%MOD+MOD)%MOD;
for(LL i=n,j=1,k=1,l=1;i>0;l+=k)
{
ans=((ans+(n%MOD)*(k%MOD)-(i%MOD)*divide2(k+j-1+j,k)%MOD)%MOD+MOD)%MOD;
if(l>=m)break;
i=min(i-1,n/(k+j));
j=k+j;
if(i==0)break;
k=min(n/i-j+1,m-j+1);
}
/*
LL ans2=0;
for(LL i=1;i<=m;i++)
ans2=(ans2+n%i)%MOD;
*/
printf("%lld\n",ans);
return 0;
}
思路:其实计算 n mod x 可以转换为 n - [n/x]*x n/x=i 是一个连续的范围 比如 10/4 =2 10/5=2 我们只要计算出 n/x=i 的一个范围 ,就可以用等差公式求和了
ps :上网查了一下 有开方的策略 好像才是正确姿势 = = 代码太丑 爆了好多次LL
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<stdio.h>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define N 100010
#define MOD 1000000007
#define LL long long
LL n,m,ans;
LL divide2(LL x,LL y)
{
if(x%2==0)
return (((x/2)%MOD)*(y%MOD))%MOD;
else
return ((x%MOD)*((y/2)%MOD))%MOD;
}
int main()
{
scanf("%lld%lld",&n,&m);
ans=((max(0LL,m-n)%MOD)*(n%MOD)%MOD+MOD)%MOD;
for(LL i=n,j=1,k=1,l=1;i>0;l+=k)
{
ans=((ans+(n%MOD)*(k%MOD)-(i%MOD)*divide2(k+j-1+j,k)%MOD)%MOD+MOD)%MOD;
if(l>=m)break;
i=min(i-1,n/(k+j));
j=k+j;
if(i==0)break;
k=min(n/i-j+1,m-j+1);
}
/*
LL ans2=0;
for(LL i=1;i<=m;i++)
ans2=(ans2+n%i)%MOD;
*/
printf("%lld\n",ans);
return 0;
}
相关文章推荐
- POJ1422 Air Raid 【DAG最小路径覆盖】
- HDU 1104 Remainder
- G面经prepare: Straight Partition of A Deck of Cards
- rails分段查询
- 身体的智能
- Ibatis中的动态SQL:isNotNull,isPropertyAvailable,isNotEmpty用法
- RAID的原理和区别
- main函数参数
- LeetCode_Climbing Stairs--爬楼梯
- [leetCode]70. Climbing Stairs
- codeforces 616E Sum of Remainders(数学公式转化 较难)
- 写的小案例中问题总结--baidumap
- svn cleanup failed–previous operation has not finished; run cleanup if it was interrupted
- USACO天梯--Barn Repair
- Climbing Stairs_爬楼梯_递归
- 无法安装vmware tools的解决方PLEASE WAIT! VMware Tools is currently being installed on your system. Dependin
- haartraing 准备(将图像统一修改尺寸)
- VS2010 error: LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt
- (Mac)Rails项目使用七牛上传及下载文件
- Studio team goals and pain points