妙题,与坐标系结合起来,曲线x*y=n上以及以下的整数坐标点总个数为小于等于n的数的约数总数,然后用x=y这条直线分成两部分,重复的部分减去。
We define the function f(x) = the number of divisors of x. Given two integers a and b (a ≤ b), please calculate f(a) + f(a+1) + ... + f(b).
Input
Two integers a and b for each test case, 1 ≤ a ≤ b ≤ 2 31 - 1. The input is terminated by a line with a = b = 0.Output
The value of f(a) + f(a+1) + ... + f(b).Sample Input
9 12 1 2147483647 0 0
Sample Output
1546475828386
#include<iostream> #include<cmath> using namespace std; long long f(long long a){ long long count=0,i; for(i=1;i*i<=a;i++){ count=count+a/i-i+1; } return count*2-(int)sqrt(a); } int main(){ long long a,b,r; while(cin>>a>>b&&a&&b){ // r=0; //for(int i=a;i<=b;i++) // r=r+f(i); cout<<f(b)-f(a-1)<<endl; //cout<<f(a)<<endl; //cout<<f(b)<<endl; } return 0; }