結果

問題 No.3179 3 time mod
ユーザー hato336
提出日時 2025-06-13 21:28:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 132 ms / 2,000 ms
コード長 1,853 bytes
コンパイル時間 403 ms
コンパイル使用メモリ 82,780 KB
実行使用メモリ 86,312 KB
最終ジャッジ日時 2025-06-14 01:38:45
合計ジャッジ時間 6,828 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 42
権限があれば一括ダウンロードができます

ソースコード

diff #

import collections,sys,math,functools,operator,itertools,bisect,heapq,decimal,string,time,random
#sys.setrecursionlimit(10**9)
#sys.set_int_max_str_digits(0)
#input = sys.stdin.readline
#n = int(input())
#a = list(map(int,input().split()))
#a = []
#s = input()
#
#for i in range(n):
#    a.append(list(map(int,input().split())))
#https://github.com/shakayami/ACL-for-python/blob/master/math.py
def inv_gcd(a,b):
    a=a%b
    if a==0:
        return (b,0)
    s=b;t=a
    m0=0;m1=1
    while(t):
        u=s//t
        s-=t*u
        m0-=m1*u
        s,t=t,s
        m0,m1=m1,m0
    if m0<0:
        m0+=b//s
    return (s,m0)
def inv_mod(x,m):
    assert 1<=m
    z=inv_gcd(x,m)
    assert z[0]==1
    return z[1]
def crt(r,m):
    assert len(r)==len(m)
    n=len(r)
    r0=0;m0=1
    for i in range(n):
        assert 1<=m[i]
        r1=r[i]%m[i]
        m1=m[i]
        if m0<m1:
            r0,r1=r1,r0
            m0,m1=m1,m0
        if (m0%m1==0):
            if (r0%m1!=r1):
                return (0,0)
            continue
        g,im=inv_gcd(m0,m1)
        u1=m1//g
        if ((r1-r0)%g):
            return (0,0)
        x=(r1-r0)//g % u1*im%u1
        r0+=x*m0
        m0*=u1
        if r0<0:
            r0+=m0
    return (r0,m0)

def floor_sum(n,m,a,b):
    ans=0
    if a<0:
        a2=a%m
        return floor_sum(n,m,a2,b)-n*(n-1)*((a2-a)//m)//2
    if b<0:
        b2=b%m
        return floor_sum(n,m,a,b2)-n*((b2-b)//m)
    if a>=m:
        ans+=(n-1)*n*(a//m)//2
        a%=m
    if b>=m:
        ans+=n*(b//m)
        b%=m
    y_max=(a*n+b)//m
    x_max=(y_max*m-b)
    if y_max==0:
        return ans
    ans+=(n-(x_max+a-1)//a)*y_max
    ans+=floor_sum(y_max,a,m,(a-x_max%a)%a)
    return ans
n = int(input())
p,q,r = map(int,input().split())
a,b,c = map(int,input().split())

x,y = crt([a,b,c],[p,q,r])
n -= x
print(n // y + 1)
0