1204. Idempotents

Time Limit: 1.0 second
Memory Limit: 16 MB
The number x is called an idempotent modulo n if
x* x = x (mod n)
Write the program to find all idempotents modulo n, where n is a product of two distinct primes p and q.

Input

First line contains the number K of test cases to consider (1 ≤ K ≤ 1000). Each of the following K lines contains one number n < 10 9.

Output

Write to the K-th line all idempotents of K-th test case in increasing order. Only nonnegative solutions bounded by n should be printed.

Sample

input output
3
                        6
                        15
                        910186311
0 1 3 4
                        0 1 6 10
                        0 1 303395437 606790875
Problem Author: Pavel Atnashev
Problem Source: USU Internal Contest, March 2002

x^2=x(mod pq)
x(x-1)=0mod(pq)
有两个解0,1。
然后p|x且q|x-1或者p|x-1且q|x
设x=ap,x-1=bq或者x=aq,x-1=bp
解方程ap-bq=1
          或aq-bp=1
输出
URAL 1204
#include<iostream>
using namespace std;
int n;
int x,y;
bool pr[1000000];
int pp[10000];
int p,q;
int E_Euclid(int a,int b,int &x,int &y)
{
    
int gcd,tmp;
    
if(b==0)
    {
        x
=1;
        y
=0;
        
return a;
    }
    
else
    {
        gcd
=E_Euclid(b,a%b,x,y);
        tmp
=x;
        x
=y;
        y
=tmp-(a/b)*y;
    }
    
return gcd;
}
int main()
{
    
int tes;
        
int i,j;
    
for(i=2;i<=100000;i++)
    {
        
if(pr[i]==0)
        {
            pp[
++pp[0]]=i;
        }
        
for(j=1;j<=pp[0]&&pp[j]*i<=100000;j++)
        {
            pr[i
*pp[j]]=1;
            
if(i%pp[j]==0)
                
break;
        }
    }
        scanf(
"%d",&tes);
    
while(tes--)
    {
        scanf(
"%d",&n);
        
for(i=1;i<=pp[0];i++)
        {
            
if(n%pp[i]==0)
            {
                p
=pp[i];
                q
=n/p;
                
break;
            }
        }
        E_Euclid(p,q,x,y);
        
if(x>0)
        {
                
while(x-q>0)
                {
                    x
-=q;
                }
        }
        
if(x<0)
        {
                
while(x<0)
                {
                    x
+=q;
                }
        }
        
int xx=x*p;
        E_Euclid(q,p,x,y);
        
if(x>0)
        {
                
while(x-p>0)
                {
                    x
-=p;
                }
        }
        
if(x<0)
        {
                
while(x<0)
                {
                    x
+=p;
                }
        }
        
int yy=x*q;
        printf(
"0 1 ");
            
if(xx>yy)
                printf(
"%d %d\n",yy,xx);
            
else
                printf(
"%d %d\n",xx,yy);
        
    }
    
return 0;
}