URAL 1204. Idempotents 擴充歐幾裡德

來源:互聯網
上載者:User

標籤:blog   http   io   ar   for   2014   html   log   sp   

題目來源:URAL 1204. Idempotents

題意:輸入n(n = p*q p,q是質數) 並且x*x=x(mod n) 求x

思路: x*x=x(mod n)  -> x*x+k*n=x -> x*(x-1)/n = k 所以 0 和 1 是一組解 因為n = p*q 且x*(x-1)%(p*q)== 0 x < n 因為x*x%n == x 模n之後才是x

1.x有p因子x-1有q因子

x%p == 0且(x-1)%q == 0

a*p == x且b*q == x-1 得到a*p-b*q == 1 gcd(p, q) == 1 用擴充歐幾裡德解出a, x ==a*p就是答按 在使他大於0

2.x-1有p因子x有q因子 x%p == 0 同上

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;void gcd(int a, int b, int& d, int& x, int& y){if(!b){d = a;x = 1;y = 0;}else{gcd(b, a%b, d, y, x);y -= x * (a/b);}}bool prime(int x){for(int i = 2; i*i <= x; i++){if(x%i == 0)return false;}return true;}int main(){int T;scanf("%d", &T);while(T--){int n;scanf("%d", &n);int p, q;for(int i = 2; i*i <= n; i++){if(n%i == 0 && prime(i) && prime(n/i)){p = i;q = n/i;break;}}int x, y, d;gcd(p, q, d, x, y);int x1 = p*x;if(x1 < 0)x1 += n;gcd(q, p, d, x, y);int x2 = q*x;if(x2 < 0)x2 += n;if(x1 > x2)swap(x1, x2);printf("%d %d %d %d\n", 0, 1, x1, x2);}return 0;}


 

URAL 1204. Idempotents 擴充歐幾裡德

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.