https://ac.nowcoder.com/acm/contest/9692/D
p进制末尾有几个0,说明问能被最大的p的几次方整除。
把p分解了,然后算有几个就行了
#include<iostream> #include<vector> using namespace std; typedef long long ll; vector<int>ins; int main() { int n,p; int t; cin>>t; while(t--) { scanf("%d %d",&n,&p); int ans = 0; ins.clear(); for(int i=2;i*i <= p;i++){ if(i*i == p){ ins.push_back(i); } else{ if(p % i == 0) ins.push_back(i); } } if(ins.size() == 0){ ans = 0; for(ll j=p;j <= n;j *= p){ ans += n/j; } printf("%d ",ans); continue; } for(int i=0;i<ins.size();i++){ int x = ins[i]; int y = p / ins[i]; int a = 0; int b = 0; for(ll j=x;j <= n;j *= x){ a += n/j; } for(ll j=y;j <= n;j *= y){ b += n/j; } if(x == y){ ans += a/2; } else{ ans += min(a,b); } } printf("%d ",ans); } return 0; }