結果

問題 No.2440 Accuracy of Integer Division Approximate Functions
ユーザー 👑 kmjpkmjp
提出日時 2023-08-29 00:15:47
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 230 ms / 2,000 ms
コード長 507 bytes
コンパイル時間 238 ms
コンパイル使用メモリ 10,836 KB
実行使用メモリ 8,404 KB
最終ジャッジ日時 2023-08-29 00:15:53
合計ジャッジ時間 5,048 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,340 KB
testcase_01 AC 198 ms
8,220 KB
testcase_02 AC 201 ms
8,268 KB
testcase_03 AC 201 ms
8,224 KB
testcase_04 AC 203 ms
7,916 KB
testcase_05 AC 227 ms
8,400 KB
testcase_06 AC 157 ms
8,392 KB
testcase_07 AC 156 ms
8,248 KB
testcase_08 AC 155 ms
8,396 KB
testcase_09 AC 158 ms
8,352 KB
testcase_10 AC 156 ms
8,332 KB
testcase_11 AC 202 ms
8,252 KB
testcase_12 AC 203 ms
8,324 KB
testcase_13 AC 202 ms
8,404 KB
testcase_14 AC 198 ms
8,320 KB
testcase_15 AC 230 ms
8,400 KB
testcase_16 AC 116 ms
8,264 KB
testcase_17 AC 115 ms
8,312 KB
testcase_18 AC 116 ms
8,260 KB
testcase_19 AC 115 ms
8,252 KB
testcase_20 AC 116 ms
8,360 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import math

def floor_sum(N,M,A,B):
	# sum(i=0...N-1) floor((A*i+B)/M)
	
	ret=0
	if B>=M:
		ret+=N*(B//M)
		B%=M;

	if A>=M:
		ret+=N*(N-1)//2*(A//M)
		A%=M
	
	Y=(A*N+B)//M
	if Y==0:
		return ret
	
	X=Y*M-B
	ret+=(N-(X+A-1)//A)*Y
	ret+=floor_sum(Y,A,M,(A-X%A)%A)
	return ret



T=int(input())
while T > 0:
	T-=1
	N,D,M,S=map(int,input().strip().split(" "))
	
	a=D*M
	b=1<<S
	
	if a==b:
		R=N
	else:
		R=min(N,D*b//abs(a-b))
	
	C=floor_sum(R+1,D,1,0)-floor_sum(R+1,b,M,0)
	R=R-abs(C)

	print(R)

0