fork download
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. #define fo(i,d,c) for(int i=d;i<=c;i++)
  4. #define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  5.  
  6. using namespace std;
  7.  
  8. int a,b;
  9. const int N=1e7;
  10. ll f[N+1];
  11. void sang_uoc(int n)
  12. {
  13. fo(i,1,n)
  14. {
  15. for(int j=i*2;j<=n;j+=i){
  16. f[j]+=i;
  17. }
  18. }
  19. }
  20. int tinh(int n)
  21. {
  22. if(n!=1){
  23. int tmp=n-1;
  24. fo(i,2,sqrt(n)){
  25. if(n%i==0){
  26. tmp-=(i+n/i);
  27. }
  28. }
  29. int sqrt_n=sqrt(n);
  30. if(sqrt_n*sqrt_n==n){
  31. tmp+=sqrt_n;
  32. }
  33. return abs(tmp);
  34. }
  35. else{
  36. return n;
  37. }
  38. }
  39. ll solve1(){
  40. ll res=0;
  41. fo(i,a,b){
  42. res+=tinh(i);
  43. }
  44. return res;
  45. }
  46. ll solve2()
  47. {
  48. ll res=0;
  49. fo(i,a,b){
  50. res+=abs(i-f[i]);
  51. }
  52. return res;
  53. }
  54. int main()
  55. {
  56. IOS;
  57. cin>>a>>b;
  58. if(b<=1e6){
  59. cout<<(ll)solve1();
  60. }
  61. else{
  62. sang_uoc(N);
  63. cout<<(ll)solve2();
  64. }
  65. return 0;
  66. }
  67.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
1