結果
| 問題 |
No.3118 Increment or Multiply
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-04-20 22:46:29 |
| 言語 | Java (openjdk 23) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 3,853 bytes |
| コンパイル時間 | 2,719 ms |
| コンパイル使用メモリ | 81,572 KB |
| 実行使用メモリ | 57,252 KB |
| 最終ジャッジ日時 | 2025-04-20 22:46:39 |
| 合計ジャッジ時間 | 10,038 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 5 WA * 30 |
ソースコード
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.NoSuchElementException;
public class Main implements Runnable {
public static void main(String[] args) {
new Thread(null, new Main(), "", 512 * 1024 * 1024).start();
}
final long mod=998244353;
long pow(long a, long n) {
if(n==0)return 1;
return pow(a*a%mod,n/2)*(n%2==1?a:1)%mod;
}
long inv(long a) {
return pow(a,mod-2);
}
long i2=inv(2);
long solve(long N, long A) {
if(A==1) {
N%=mod;
long ret=N*(N-1)%mod*i2%mod;
return (ret+mod)%mod;
}
long b=1;
int e=0;
while(b<=N/A) {
b*=A;
++e;
}
long[] x=new long[e+10];
long[] power=new long[e+10];
long[] res=new long[e+10];
x[0]=N;
power[0]=1;
for(int i=1;i<x.length;++i) {
x[i]=x[i-1]/A;
}
for(int i=0;i<x.length;++i)x[i]%=mod;
for(int i=1;i<power.length;++i) {
power[i]=power[i-1]*(A%mod)%mod;
}
for(int i=1;i<res.length;++i) {
res[i]=(res[i-1]+x[i-1]%A)%mod;
}
//x[i]=[N/A^i]
long ans=0;
for(int i=0;i<=e;++i) {
// A^{e-i-1} <= [N/A^{i+1}] < z < A^{e-i}
if(i!=e) {
long d=(power[e-i]-x[i+1]-1)%mod;
if(d>0) {
ans+=d*(d+1)%mod*i2%mod;
ans%=mod;
ans+=d*(x[i]-power[e-i])%mod;
ans%=mod;
ans+=d*(i+res[i])%mod;
ans%=mod;
}
}
//A^{e-i} <= z <= [N/A^i] < A^{e-(i-1)}
ans+=(x[i]-power[e-i])%mod*(x[i]-power[e-i]+1)%mod*i2%mod;
ans+=(i+res[i])*(x[i]-power[e-i]+1)%mod;
ans%=mod;
}
return (ans+mod)%mod;
}
public void run() {
FastScanner sc=new FastScanner();
PrintWriter pw=new PrintWriter(System.out);
int T=sc.nextInt();
for(int t=0;t<T;++t) {
long N=sc.nextLong();
long A=sc.nextLong();
pw.println(solve(N,A));
}
//
// tr(solve(5,2));
pw.close();
}
void tr(Object...objects) {System.out.println(Arrays.deepToString(objects));}
}
class FastScanner {
private final InputStream in = System.in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int buflen = 0;
private boolean hasNextByte() {
if (ptr < buflen) {
return true;
}else{
ptr = 0;
try {
buflen = in.read(buffer);
} catch (IOException e) {
e.printStackTrace();
}
if (buflen <= 0) {
return false;
}
}
return true;
}
private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
public String next() {
if (!hasNext()) throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while(isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long nextLong() {
if (!hasNext()) throw new NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while(true){
if ('0' <= b && b <= '9') {
n *= 10;
n += b - '0';
}else if(b == -1 || !isPrintableChar(b)){
return minus ? -n : n;
}else{
throw new NumberFormatException();
}
b = readByte();
}
}
public int nextInt() {
long nl = nextLong();
if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
return (int) nl;
}
public double nextDouble() { return Double.parseDouble(next());}
}