fork(1) download
  1. #include<stdio.h>
  2. #include<math.h>
  3.  
  4. using namespace std;
  5. typedef unsigned long long ull;
  6. typedef long long ll;
  7.  
  8. const int N=200005;
  9.  
  10. int T,S,pr[N],pc;
  11. ll n,num[N],m,K;
  12. ull g[N];
  13. bool fl[N];
  14. inline int ID(ll x){return x<=S?x:m-n/x+1;}
  15.  
  16. ull f(ll n,int i){
  17. if(n<1||pr[i]>n)return 0;
  18. ull ret=g[ID(n)]-(i-1)*(K+1);
  19. while((ll)pr[i]*pr[i]<=n){
  20. int p=pr[i];
  21. ull e=K+1,t=n/p;
  22. while(t>=p)ret+=f(t,i+1)*e+e+K,t/=p,e+=K;
  23. i++;
  24. }
  25. return ret;
  26. }
  27.  
  28. ull solve(ll n){
  29. int i,p,x;ull y;
  30. S=sqrt(n);
  31. while((ll)S*S<=n)S++;
  32. while((ll)S*S>n)S--;
  33. while(m)num[m--]=0;
  34. for(i=1;i<=S;i++)num[++m]=i;
  35. for(i=S;i>=1;i--)if(n/i>S)num[++m]=n/i;
  36. for(i=1;i<=m;i++)g[i]=num[i]-1;
  37. x=1;y=0;
  38. for(p=2;p<=S;p++)if(g[p]!=g[p-1]){
  39. while(num[x]<(ll)p*p)x++;
  40. for(i=m;i>=x;i--)g[i]-=g[ID(num[i]/p)]-y;
  41. y++;
  42. }
  43. for(i=1;i<=m;i++)g[i]*=K+1;
  44. return f(n,1)+1;
  45. }
  46.  
  47. int main(){
  48. int i,j;
  49. for(i=2;i<N;i++)if(!fl[i]){
  50. pr[++pc]=i;
  51. for(j=i+i;j<N;j+=i)fl[j]=1;
  52. }
  53. for(scanf("%d",&T);T--;){
  54. scanf("%lld%lld",&n,&K);
  55. printf("%llu\n",solve(n));
  56. }
  57. return 0;
  58. }
Success #stdin #stdout 0s 4548KB
stdin
Standard input is empty
stdout
Standard output is empty