{"id":338,"date":"2012-05-01T17:10:13","date_gmt":"2012-05-01T09:10:13","guid":{"rendered":"http:\/\/www.shuizilong.com\/house\/?p=338"},"modified":"2012-08-01T17:12:46","modified_gmt":"2012-08-01T09:12:46","slug":"spoj-3179-congruence-equation","status":"publish","type":"post","link":"https:\/\/www.shuizilong.com\/house\/archives\/spoj-3179-congruence-equation\/","title":{"rendered":"SPOJ 3179. Congruence Equation"},"content":{"rendered":"<h3>Brief description: <\/h3>\n<p>\u2026 n \u5143\u7ebf\u6027\u65b9\u7a0b\u7ec4\u7684\u6c42\u89e3\u2026<br \/>\n<!--more--><\/p>\n<h3>Analysis: <\/h3>\n<p>.. (n + 1) \u6b21 exGcd .. \u5224\u65ad\u662f\u5426\u6709\u89e3 ..<br \/>\n\u4e4b\u540e\u2026 \u7cfb\u6570\u5411\u91cf\u7528\u521a\u624d\u7684\u4e2d\u95f4\u7ed3\u679c\u53cd\u4ee3\u51fa\u5373\u53ef\u3002<\/p>\n<pre class=\"brush: cpp; collapse: true; first-line: 1; light: false; title: SPOJ 3179. Congruence Equation.cpp; toolbar: true; notranslate\" title=\"SPOJ 3179. Congruence Equation.cpp\">\r\n#include &lt;iostream&gt;\r\n#include &lt;cmath&gt;\r\n \r\nusing namespace std;\r\n \r\n#define DO(N) while(N--)\r\n#define REP(I, N) for (int I=0;I&lt;int(N);++I)\r\n#define FOR(I, A, B) for (int I=int(A);I&lt;int(B);++I)\r\n#define DWN(I, B, A) for (int I=int(B-1);I&gt;=int(A);--I)\r\n#define FOR_1(I, A, B) for (int I=int(A);I&lt;=int(B);++I)\r\n#define DWN_1(I, B, A) for (int I=int(B);I&gt;=int(A);--I)\r\n \r\ntemplate&lt;class T&gt; inline void RD(T &amp;x){\r\n    char c; for (c = getchar(); c &lt; '0'; c = getchar()); x = c - '0';\r\n    for (c = getchar(); c &gt;= '0'; c = getchar()) x = x * 10 + c - '0';\r\n}\r\ntemplate&lt;class T&gt; inline void RD(T &amp;x0, T &amp;x1){RD(x0), RD(x1);}\r\ntemplate&lt;class T&gt; inline void RD(T &amp;x0, T &amp;x1, T &amp;x2){RD(x0), RD(x1), RD(x2);}\r\n \r\ntypedef long long LL;\r\n \r\nconst int N = 100;\r\nint a&#x5B;N], x&#x5B;N], y&#x5B;N];\r\nint n, b, c, d, t, m, _;\r\n \r\n \r\ninline void INC(int &amp;a, int b){\r\n    a += b; if (a &gt;= m) a -= m;\r\n}\r\n \r\ninline void DEC(int &amp;a, int b){\r\n    a -= b; if (a &lt; 0) a += m;\r\n}\r\n \r\ninline void MUL(int &amp;a, int b){\r\n    a = int(LL(a) * b % m);\r\n}\r\n \r\ninline int mul(int a, int b){\r\n    return int(LL(a) * b % m);\r\n}\r\n \r\ninline void exGcd(int m, int n, int &amp;d, int&amp; a, int &amp; b){\r\n\tint xx = 0, yy = 1, x = 1, y = 0, q;\r\n\twhile (true){\r\n\t\tq = m \/ n, m %= n;\r\n        if (!m) {d = n, a = xx, b = yy; return;}\r\n\t\tDEC(x, mul(q, xx)), DEC(y, mul(q, yy));\r\n        q = n \/ m, n %= m;\r\n        if (!n) {d = m, a = x, b = y; return;}\r\n        DEC(xx, mul(q, x)), DEC(yy, mul(q, y));\r\n\t}\r\n}\r\n \r\n \r\nvoid init(){\r\n    RD(n); REP(i, n) RD(a&#x5B;i]);\r\n    RD(b, m);\r\n \r\n    b %= m;\r\n    REP(i, n) a&#x5B;i] %= m;\r\n \r\n}\r\n \r\nvoid solve(){\r\n \r\n    d = a&#x5B;0], --n;\r\n \r\n    REP(i, n){\r\n        exGcd(d, a&#x5B;i+1], d, x&#x5B;i], y&#x5B;i]); \r\n    }\r\n \r\n    exGcd(d, m, d, t, _);\r\n \r\n    if (b % d) {\r\n        printf(&quot;NO\\n&quot;);\r\n        return;\r\n    }\r\n \r\n    c = b \/ d;\r\n \r\n    MUL(c, t), MUL(x&#x5B;n-1], c), MUL(y&#x5B;n-1], c);\r\n \r\n    DWN(i, n, 1){\r\n        x&#x5B;i+1] = y&#x5B;i], MUL(x&#x5B;i-1], x&#x5B;i]), MUL(y&#x5B;i-1], x&#x5B;i]);\r\n    }\r\n    x&#x5B;1] = y&#x5B;0];\r\n \r\n    FOR(i, 0, n){\r\n        printf(&quot;%d &quot;, x&#x5B;i]);\r\n    }\r\n    printf(&quot;%d\\n&quot;, x&#x5B;n]);\r\n}\r\n \r\n \r\nint main(){\r\n    \/\/freopen(&quot;in.txt&quot;, &quot;r&quot;, stdin);\r\n    \/\/freopen(&quot;out.txt&quot;, &quot;w&quot;, stdout);\r\n \r\n    int T; RD(T);\r\n    DO(T){\r\n        init(); solve();\r\n    }\r\n}\r\n<\/pre>\n<h3>External link: <\/h3>\n<p><a href=\"http:\/\/www.spoj.pl\/problems\/DPEQN\/\">http:\/\/www.spoj.pl\/problems\/DPEQN\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Brief description: \u2026 n \u5143\u7ebf\u6027\u65b9\u7a0b\u7ec4\u7684\u6c42\u89e3\u2026<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[21],"tags":[],"class_list":["post-338","post","type-post","status-publish","format-standard","hentry","category-spoj"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2tdP7-5s","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/338","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/comments?post=338"}],"version-history":[{"count":0,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/338\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/media?parent=338"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/categories?post=338"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/tags?post=338"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}