📄 toj_2861.cpp
字号:
/*2861. Divisors Time Limit: 1.0 Seconds Memory Limit: 65536KTotal Runs: 374 Accepted Runs: 153We 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).InputTwo integers a and b for each test case, 1 ≤ a ≤ b ≤ 231 - 1. The input is terminated by a line with a = b = 0.OutputThe value of f(a) + f(a+1) + ... + f(b).Sample Input9 121 21474836470 0Sample Output1546475828386HintFor the first test case:9 has 3 divisors: 1, 3, 9.10 has 4 divisors: 1, 2, 5, 10.11 has 2 divisors: 1, 11.12 has 6 divisors: 1, 2, 3, 4, 6, 12.So the answer is 3 + 4 + 2 + 6 = 15.Source: TJU Team Selection Contest 1*/#include<cstdio>#include<cmath>int main(){ int a , b , i , j , k , sqrtI; long long result; while ( scanf( "%d%d" , &a , &b ) != EOF && a != 0 && b != 0 ) { result = 0; for ( i = a; i <= b; i++ ) { result += 2; sqrtI = static_cast< int >( sqrt( i ) ); for ( j = 2; j <= sqrtI; j++ ) { if ( i % j == 0 ) result += 2; } if ( sqrtI * sqrtI == i ) result --; } printf( "%ld\n" , result ); } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -