//Place your snippets for cpp here. Each snippet is defined under a snippet name and has a prefix, body and 
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the 
// same ids are connected.
// prefix 的格式要求是 acm-[一级标题编号].[二级标题编号][该代码片段的英文简称]
// description 的格式要求是 "插入 [该代码片段的中文名]"

{
    "EXCRT扩展中国剩余定理": {
        "prefix": "acm-7.1P不一定两两互质",
        "body": [
            "#include<cstdio>",
            "#define LL long long",
            "#define Re register LL",
            "const int N=1e5+3;",
            "LL n,ans,lcm=1,a[N],m[N];",
            "inline void in(Re &x){",
            "    int f=0;x=0;char c=getchar();",
            "    while(c<'0'||c>'9')f|=c=='-',c=getchar();",
            "    while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();",
            "    x=f?-x:x;",
            "}",
            "inline LL exgcd(Re a,Re b,Re &x,Re &y){",
            "\tif(!b){x=1,y=0;return a;}",
            "\tRe gcd=exgcd(b,a%b,x,y);Re x0=x,y0=y;",
            "\tx=y0,y=x0-a/b*y0;return gcd;",
            "}",
            "inline LL cf(Re x,Re k,Re P){",
            "\tRe s=0;",
            "\twhile(k){",
            "\t\tif(k&1)(s+=x)%=P;",
            "\t\t(x+=x)%=P,k>>=1;",
            "\t}",
            "\treturn s;",
            "}",
            "int main(){",
            "//    freopen(\"123.txt\",\"r\",stdin);",
            "    in(n);",
            "    for(Re i=1;i<=n;++i)in(m[i]),in(a[i]),lcm*=m[i];",
            "    ans=a[1]%m[1],lcm=m[1];",
            "    for(Re i=2;i<=n;++i){//ans+lcm*k=a[i](mod m[i]) -> lcm*k=a[i]-ans(mod m[i]) -> lcm*k+m[i]*y=a[i]-ans",
            "\t\tRe c=((a[i]-ans)%m[i]+m[i])%m[i],x,y;",
            "\t\tRe gcd=exgcd(lcm,m[i],x,y);",
            "\t\tRe k=cf(x,c/gcd,m[i]/gcd);",
            "\t\tans+=lcm*k,lcm*=m[i]/gcd,ans=(ans%lcm+lcm)%lcm;",
            "\t}",
            "\tprintf(\"%lld\\n\",ans);",
            "}",
            ""
        ],
        "description": "插入 EXCRT扩展中国剩余定理"
    }
}