Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

【No.3133 法B逆元】Pythonでの簡単な実装

Last updated at Posted at 2025-05-02

問題ページ

yukicoder No.3133 法B逆元 ☆1.5・数学 整数論 算術

解説

$Ni = 1 ~ (\mathrm{mod} ~ B)$ なる非負整数 $i$ 、つまり $N^{-1} ~ (\mathrm{mod} ~ B)$ は Python の pow 関数で求めることができます。

ただし、逆元が存在しない場合 ValueError が投げられるので、それをキャッチする必要があることに注意してください。

N, B = map(int, input().split())

try:
    print(pow(N, -1, B))
except ValueError:
    print("NaN")
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?