//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 的格式要求是 "插入 [该代码片段的中文名]"

{
    "CRT中国剩余定理 (P两两互质)": {
        "prefix": "acm-7.1同余方程组",
        "body": [
            "#include<cstdio>",
            "#define LL long long",
            "#define Re register LL",
            "const int N=13;",
            "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 void exgcd(Re a,Re b,Re &x,Re &y){",
            "\tif(!b){x=1,y=0;return;}",
            "\texgcd(b,a%b,x,y);Re x0=x,y0=y;",
            "\tx=y0,y=x0-a/b*y0;return;",
            "}",
            "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];",
            "    for(Re i=1;i<=n;++i){",
            "\t\tRe x,y,M=lcm/m[i];exgcd(M,m[i],x,y);//M[i]*inv+m[i]*y=1",
            "\t\tx=(x%m[i]+m[i])%m[i],(ans+=cf(cf(a[i],M,lcm),x,lcm))%=lcm;",
            "\t}",
            "\tprintf(\"%lld\\n\",ans);",
            "}",
            ""
        ],
        "description": "插入 CRT中国剩余定理 (P两两互质)"
    }
}