CodeChef:Desik And Divisors(数学 & 二分 & 思维)
来源:互联网 发布:剑灵捏脸数据分享 编辑:程序博客网 时间:2024/04/28 18:42
Given two integers L and K. You are to find the minimum integer R such that Sum of the number of divisors of all numbers from L to R is greater than K.
Input
- First Line specifies an integer T, the number of test cases.
- Every Test case consists of two integers L and K as described in problem statement.
Output
- For every test case print, the value of R in a new line and print - 1 if R - L > 10 ^ 9.
Constraints
Should contain all the constraints on the input data that you may have. Format it like:
- 1 ≤ T ≤ 20
- 1 ≤ L ≤ 10 ^ 9
- 1 ≤ K ≤ 10 ^ 18
Example
Input:25 205 28Output:1113题意:给出L和K,要求找出最小的R,使得[L,R]区间的数的因子数目之和大于K,若R-L>1e9就输出-1。
思路:二分R,验证1~R因子数目和减去1~L-1的因子数目和是否>K。计算1~X的因子数目和参考代码,其实很好理解枚举整除<=sqrt X的因子数,乘二就是总的因子数。但是有些两个因子都在sqrt X里面刚好重复计算了两次,再减去sqrt X * sqrt X就行。
# include <iostream># include <cmath># include <cstdio>using namespace std;typedef long long LL;LL cal(LL x){ LL res = 0, sq = sqrt(x); for(int i=1; i<=sq; ++i) res += x/i; return res*2-sq*sq;}int main(){ LL l, k; int T; scanf("%d",&T); while(T--) { scanf("%lld%lld",&l,&k); if(cal(l+1e9)-cal(l-1) <= k) { puts("-1"); continue; } LL r = 1e9+l, left = cal(l-1); while(l < r) { LL mid = l+r>>1; if(cal(mid)-left>k) r = mid; else l = mid+1; } printf("%lld\n",r); } return 0;}
阅读全文
0 0
- CodeChef:Desik And Divisors(数学 & 二分 & 思维)
- codechef-Chef and Prime Divisors
- codechef(MAY15)第三题Chef and Prime Divisors
- CodeChef:Chef and Subsequences(思维 & dfs)
- Chef and Prime Divisors (CodeChef CHAPD
- CodeChef:Chef and Problems(分块 & 二分)
- CodeChef:A temple of Snakes(思维 & 二分)
- 【codechef】Chef and Prime Divisors(判断A能否被B的所有素因子整除)
- CodeChef:Hasan and boring classes(组合数学 & 字符串)
- UVa 294 - Divisors(数学)
- hdu6069 Counting Divisors(数学)
- CodeChef:Chef Anup(数学)
- CodeChef STRSUB(dp+二分)
- codeforce The Meaningless Game(二分+数学思维)
- hdu5976 Detachment(逆元+二分优化+数学思维)
- CF 111B Petya and Divisors【想法题+二分(可省略)】
- Code Forces 342 A. Xenia and Divisors 思维+暴力
- Codeforces 301D Yaroslav and Divisors 【树状数组 + 思维】
- java HttpServlet 之 会话Cookie和Session
- Java提高篇(三四)-----fail-fast机制
- JAVA WEB项目中的路径问题。
- K
- 使用Mybatis-Generator自动生成代码
- CodeChef:Desik And Divisors(数学 & 二分 & 思维)
- You're Given a String... CodeForces
- [Unity]如果AssetStore的下载无效怎么办
- [hdu1599]find the mincost route
- 数据库
- 网络请求数据之XUtils和Volley
- VS2013+QT5.8.0 过程记录
- VS2013+QT5.8.0 过程记录
- hduoj 2010