首页 > 其他分享 >CodeForces - 616E Sum of Remainders (数论)大数取余求和 好题

CodeForces - 616E Sum of Remainders (数论)大数取余求和 好题

时间:2023-04-19 15:32:47浏览次数:52  
标签:Remainders nn Sum 616E Output ans Input include sum

CodeForces - 616E Sum of Remainders
Time Limit: 2000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u

Submit Status

Description

Calculate the value of the sum: nmod1 + nmod2 + nmod3 + ... + nmodm. As the result can be very large, you should print the value modulo 109 + 7 (the remainder when divided by 109 + 7).

The modulo operator amodb stands for the remainder after dividing a by b. For example 10mod3 = 1.

Input

The only line contains two integers n, m (1 ≤ n, m ≤ 1013) — the parameters of the sum.

Output

Print integer s — the value of the required sum modulo 109 + 7.

Sample Input

Input 3 4 Output 4 Input 4 4 Output 1 Input 1 1 Output 0

Source

Educational Codeforces Round 5 //题意就不说了,直接上大神的思路了,很6的方法

题意:求解 ∑ m i=1 nmodi 。

思路:我们化简 nmodi=n−n/i∗i 。
这样原式 =n∗m−∑ m i=1 (n/i∗i)
首先 m<=n √ 时,直接暴力,对于剩下的,我们可以分块来搞,总会存在一段连续的值使得 n/i 为定值,那么我们找到这个区间就可以了。不过貌似在除以2的时候,会出问题,我直接上逆元就过了。

#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<queue>
#include<algorithm>
#define IN __int64
#define M 1000000007
using namespace std;
IN kp(IN a, IN n) 
{
	IN ans = 1;
	while(n)
	{
		if(n&1)
			ans=ans*a%M;
		a=a*a%M;
		n>>=1;
	}
	return ans;
}
int main()
{
	IN n,m,sum,ans1,i,j,nn;
	while(scanf("%I64d%I64d",&n,&m)!=EOF)
	{
		sum=n%M*(m%M)%M;
		nn=sqrt(n);
		ans1=0;
		for(i=1;i<=min(m,nn);i++)
			ans1=(ans1+n/i%M*i%M)%M;
		if(m>nn)
		{
			if(nn*nn==n)
				nn--;
			for(i=1;i<=nn;i++)
			{
				IN r=min(m,n/i);
				IN l=n/(i+1)+1;
				if(l>r||r<=nn)
					continue;
				ans1=(ans1+(l+r)%M*((r-l+1)%M)%M*kp(2,M-2)%M*i%M)%M;
			}
		}
		printf("%I64d\n",(sum-ans1+M)%M);
	}
	return 0;
}


 

标签:Remainders,nn,Sum,616E,Output,ans,Input,include,sum
From: https://blog.51cto.com/u_16079508/6206441

相关文章

  • Sumsets UVA - 10125
     一个集合中需要找到a,b,c,d,使得a+b+c=d 枚举a,b,计算a+b,存在map里再枚举d,c,计算d-c是否存在d-c==a+b #include<iostream>#include<map>#include<algorithm>#include<vector>usingnamespacestd;constintN=1e3+2;//a+b==d-c#definepiipair<......
  • HNU2019 Summer Training 3 E. Blurred Pictures
    E.BlurredPicturestimelimitpertest2secondsmemorylimitpertest256megabytesinputstandardinputoutputstandardoutputDamonlovestotakephotosoftheplaceshevisitsduringhistravels,toputthemintoframes.Allofhisphotosarei......
  • percona-toolkit工具:使用pt-table-checksum检查MySQL主从库的差异
    环境介绍CentOS7.6MySQL5.7PerconaToolkit3.4.0 下载并安装PerconaToolkit从WEB端下载https://www.percona.com/downloads或者通过wget下载[root]#wgethttps://downloads.percona.com/downloads/percona-toolkit/3.5.2/binary/redhat/7/x86_64/percona-toolkit-3.......
  • UVa 112 Tree Summing (scanf()去空格&递归&二叉树遍历)
    112-TreeSummingTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=48BackgroundLISPwasoneoftheearliesthigh-levelprogramminglanguagesand,withFORTRAN,isoneoft......
  • UVa 10783 Odd Sum (water ver.)
    10783-OddSumTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=19&page=show_problem&problem=1724Givenarange [a, b],youaretofindthesummationofalltheoddintegersinthisran......
  • Java8 - sum求和,将 List 集合转为 Map,key去重(groupingBy),sorted排序
    Java8-sum求和,将List集合转为Map,key去重(groupingBy),sorted排序packagecom.example.core.mydemo.java8;publicclassGoodsPriceDTO{privateIntegerid;privateStringgoodName;privateIntegeramount;//重写toString方法,System可以打印输出......
  • Sum of Consecutive Prime Numbers UVA - 121
     #include<iostream>#include<cstring>#include<cmath>#include<algorithm>usingnamespacestd;constintM=1e4+33;intb[M],c[M],tot;ints[M];voidinit(inttop){memset(b,1,sizeofb);b[1]=0;inti,j;......
  • Sum of Different Primes UVA - 1213
     选择K个质数,使它们的和等于N。问有多少种方案?例如,n=24,k=2时有3种方案:5+19=7+17=11+13=24 #include<iostream>#include<cstring>#include<cmath>#include<algorithm>usingnamespacestd;constintM=1e6+33;intb[M],c[M],tot;intn,m,f[2000][20];......
  • Do you know the bitwise sum sample demonstrated in "Neural Networks and Deep Lea
    Doyouknowthebitwisesumsampledemonstratedin"NeuralNetworksandDeepLearning"byautor MichaelNielsen?Yes,Iamfamiliarwiththebitwisesumexampledemonstratedin"NeuralNetworksandDeepLearning"byMichaelNielsen......
  • [ARC127D] Sum of Min of Xor 题解
    先把\(i\)对\(j\)的约束去掉。没有\(\min\)的情况是trival的,发现瓶颈在于如何比较两个数之间的大小。可以发现,对两个二进制数,我们本质上是想要找到它们第一个不同的位置。于是考虑从最高位开始,将\((a_i,b_i)\)按最高位分组为\((0,0),(0,1),(1,0),(1,1)\)四组。每组内......