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 }
时间: 2024-08-02 17:32:02

Timus 1204 Idempotents的相关文章

URAL 1204. Idempotents 扩展欧几里德

题目来源: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 得到

URAL 1204. Idempotents (扩展欧几里得)

题目链接 题意 : 给你一个同余方程, x*x ≡ x  (mod n),让你求出所有的小于n的x. 思路 : 先来看同余的概念 :给定一个正整数m,如果两个整数a和b满足a-b能被m整除,即m|(a-b),那么就称整数a与b对模m同余,记作a≡b(mod m).对模m同余是整数的一个等价关系. 因此题目中给定的式子可以写成:(x*x-x)/n=k.也就是说(x*x-x)是n的整数倍,取余n是0. 因为n=p*q,而且gcd(p,q)=1 ;所以上式可以写为,x*(x-1)/(p*q)=k. 让

hzau 1204 Escape from the Darkness

1204: Escape from the Darkness Time Limit: 1 Sec  Memory Limit: 1280 MBSubmit: 93  Solved: 3[Submit][Status][Web Board] Description Xiao Ming, a high school student, learnt blackbody radiation from the physics class. The black body on the book is ind

Timus 1049 Brave Balloonists

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1049 题目描述: 题目大意为给定10个数,然后求这10个数之积所对应的数的所有正因子的个数N的个位数. 那么直接对10个数进行质因数分解即可.(注意%10) 1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 using namespace std ; 5 6 const int MAXM

Timus OJ 1057 数位dp

http://acm.timus.ru/problem.aspx?space=1&num=1057 1057. Amount of Degrees Time limit: 1.0 second Memory limit: 64 MB Create a code to determine the amount of integers, lying in the set [X;Y] and being a sum of exactlyK different integer degrees of B.

timus 1547. Password Search【题意思路+大数模板】

题目地址传送门:URAL 1547 这道题需要用到大数的很多模板,推荐大家去刷刷! 题目大意:Vova忘记了在Timus OJ上面的密码了,密码是由小写字母(a~z)组成的,他只知道密码长度不大于n位,现在他需要用m台数据处理器对密码进行检索,其中检索顺序需要满足字典序.比如他的密码长度不大于2,那就需要依次检索a,b,..........,y,z,aa,ab,..........,zy,zz.输出每台数据检索器的检索区间,使得总的检索效率可以达到最高. 已知密码的总可能数不少于数据处理器个数.

Timus 2005. Taxi for Programmers 题解

The clock shows 11:30 PM. The sports programmers of the institute of maths and computer science have just finished their training. The exhausted students gloomily leave their computers. But there's something that cheers them up: Misha, the kind coach

Timus 2070 Interesting Numbers

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=2070 题目描述: 题目大意是给定范围[L, R],求该范围中interesting数的个数. interesting数的定义为: 1.素数 2.不为素数且其正因子的个数不为素数个(如:6: 1,2,3,6) 计算正因子的个数一般使用的是分解质因数了,若 n = p1^a1 * p2^a2 * ... pm^am 那么正因子的个数为 k = (a1+1) * (a2+1) * ... * (am

poj 1204 Word Puzzles(字典树)

题目链接:http://poj.org/problem?id=1204 思路分析:由于题目数据较弱,使用暴力搜索:对于所有查找的单词建立一棵字典树,在图中的每个坐标,往8个方向搜索查找即可: 需要注意的是查找时不能匹配了一个单词就不在继续往该方向查找,因为在某个坐标的某个方向上可能会匹配多个单词,所以需要一直 查找直到查找到该方向上最后一个坐标: 代码如下: #include <cstdio> #include <cstring> #include <iostream>