公式没搞懂。。。(( [2*n/p]-2*[n/p] ) + ( [2*n/p^2]-2*[n/p^2] ) +...+ ( [2*n/p^t]-2*[n/p^t] ))其中t = [logp(2*n)]
注意精度
代码如下:
#include<iostream>
#include<stdio.h>
#include<cmath>
using namespace std;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,p;
double temp;
int cnt = 1;
int t,ans;
scanf("%d%d",&n,&p);
temp = log10(2.0*n)*1.0/log10(p);
t = int(temp);
//printf("temp = %.2lf\n",temp);
//printf("t = %d\n",t);
ans = 0;
for(int i = 1;i <= t;i++)
{
cnt = cnt*p;
ans += (int)(2.0*n/cnt) - 2*(int)(n/cnt);
}
printf("%d\n",ans);
}
return 0;
}