Timus 1204 Idempotents

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1204

题目描述:

  x*x ≡ x (mod n) ,求解所有满足该式的x值,其中n为两个不同的素数的积,并且有x<n;

那么假设n=p*q,且 x mod n = x, 然后易得到 x*x = a*n + x,则 x(x-1) = a*n, 则必有两个解0和1

故剩余情况等价于求 a1*p + a2*q = 1的解,其中a1*a2=-a(a !=0 )

为扩展的欧几里得了,其中为正的一项为解,还有一个解即增一次即可,两次则会超过n,故有且仅有4个解。

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 using namespace std ;
 5 
 6 const int MAXM = 100000 ;
 7 int k, n ;
 8 int prims[MAXM], totals ;
 9 bool is_prim[MAXM] ;
10 
11 void init(){
12     memset(is_prim, true, sizeof(is_prim)) ;
13     totals = 0 ;
14     for( int i = 2; i < MAXM; i++ ){
15         if( is_prim[i] ){
16             for( int j = 2; j*i < MAXM; j++ )    is_prim[i*j] = false ;
17             prims[totals++] = i ;
18         }
19     }
20 }
21 
22 void extGcd(int& x, int& y, int p, int q){
23     if( q == 0 ){
24         x = 1, y = 0 ;
25         return ;
26     }
27     extGcd(x, y, q, p%q) ;
28     int t = y ;
29     y = x - (p/q)*y ;
30     x = t ;
31 }
32 
33 void solve(){
34     cout << "0 1 " ;
35     int p, q ;
36     for( int i = 0; i < totals && prims[i]*prims[i] < n; i++ ){
37         if( n % prims[i] == 0 )    p = prims[i], q = n / prims[i] ;
38     }
39     int x, y ;
40     extGcd(x, y, p, q) ;
41     if( x > 0 ){
42         cout << x*p << " " << (y+p)*q << endl ;
43     }else{
44         cout << y*q << " " << (x+q)*p << endl ;
45     }
46 }    
47 
48 int main(){
49     //freopen("1234.txt", "r", stdin) ;
50     cin >> k ;
51     init() ;
52     while( k-- ){
53         scanf("%d", &n) ;
54         solve() ;
55     }
56     return 0 ;
57 }

 

转载于:https://www.cnblogs.com/be-saber/p/5374570.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值