[牛客]第三届超越杯程序设计团体赛重现赛Find And Cal
#include<bits/stdc++.h>
#define ll long long
using namespace std;
inline ll read(){
char ch=getchar();int s=0,w=1;
while(ch<48||ch>57){
if(ch=='-')w=-1;ch=getchar();
}
while(ch>=48&&ch<=57){
s=(s<<1)+(s<<3)+ch-48;ch=getchar();
}
return s*w;
}
inline void write(ll x){
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
}
inline ll qmul(ll a,ll b,ll mod){//a*b mod mod
ll ans=0;
while(b){
if (b&1)ans=(ans+a)%mod;
b>>=1;
a=a<<1%mod;
}
return ans;
}
inline ll qpow(ll x,ll n,ll mod){//x^n mod mod
x%=mod;
ll ans=1;
while(n){
if (n&1)ans=qmul(ans,x,mod);
n>>=1;
x=qmul(x,x,mod);
}
return ans;
}
bool fermat_test(ll p,ll a){
//a^(p-1)≡1(mod p)a为小于p的任意安全检测数
if(qpow(a,p-1,p)==1)return true;
else return false;
}
const int maxn=34000;
int prime[maxn+1],cnt;
bool st[maxn+1];//0表示不是合数,即为素数
int factor[100];
const ll mod=998244343;
int main(){
ll T;
T=read();
for(int i=2;i<=maxn;i++){
if(st[i]==0)prime[cnt++]=i;
for(int j=0;prime[j]*i<=maxn;j++){
st[i*prime[j]]=true;
if(i%prime[j]==0)break;
}
}
while(T--){
ll x,y;
x=read(),y=read();
++x;
while(!fermat_test(x,2))++x;
ll ans=0;
for(int j=0;prime[j]*prime[j]<=y;j++){
if(y%prime[j]==0){
int cnt=0,x1=x,cmp=0;
while(y%prime[j]==0)++cnt,y/=prime[j];
while(x1){
cmp+=x1/prime[j];
x1/=prime[j];
}
cmp/=cnt;
if(cmp>ans)ans=cmp;
}
}
if(y>1){
int cmp=0;
while(x){
cmp+=x/y;
x/=y;
}
if(cmp>ans)ans=cmp;
}
write(ans%mod);putchar('\n');
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=998244353;
ll mul(ll a,ll b,ll m) {
ll res=0; a%=m,b%=m;
for(;b;a=(a+a)%m,b>>=1)
if(b&1) res=(res+a)%m;
return res;
}
ll qp(ll x,ll n,ll m){
ll res=1;x%=m;
for(;n;x=mul(x,x,m),n>>=1)
if(n&1) res=mul(res,x,m);
return res%m;
}
bool Miller_Robin(ll a,ll n) {
ll x=n-1,y; int t=0;
while((x&1)==0) x>>=1,t++;
x=qp(a,x,n);
for(int i=1;i<=t;++i){
y=mul(x,x,n);
if(y==1&&x!=1&&x!=n-1)
return true;
x=y;
}
return y!=1?true:false;
}
bool isprime(ll x) {
if(x==2||x==3||x==7||x==61||x==24251)
return true;
if(!(x&1)||!(x%3)||!(x%61)||!(x%24251))
return false;
if(x%6!=1&&x%6!=5) return false;
int a[5]={2,3,7,61,24251};
for(int i=0;i<5;++i){
if(a[i]>=x) break;
if(Miller_Robin(a[i],x))
return false;
}
return true;
}
int main(){
int t,y; ll x; scanf("%d",&t);
while(t--){
scanf("%lld%d",&x,&y);
++x;
while(!isprime(x)) ++x;
ll ans=0;
while(x) ans=(ans+x/y)%mod,x/=y;
printf("%lld\n",ans);
}
return 0;
}