CERC2017 F: Faulty Factorial 简单数论题

时间:2021-10-14 10:44:38

CERC2017 F: Faulty Factorial 简单数论题

 

 

 

 1 #include  <iostream>
 2 using namespace std;
 3 #define ll long long 
 4 const int N  = 10000006;
 5 ll n,p,r;
 6 ll poww(ll a,ll b){
 7     ll ans  =1ll;
 8     while(b){
 9         if(b&1) ans =ans*a%p;
10         b>>=1;
11         a=a*a%p;
12     }
13     return ans%p;
14 }
15 int  main()
16 {
17     //每次都要注意-1 -1的情况
18     scanf("%lld%lld%lld",&n,&p,&r);
19     if(n>=2*p){
20         if(r==0){
21             printf("%lld 1\n",p+1);
22         }
23          else{
24              printf("-1 -1\n");
25          }
26     }
27     else if(n>=p){
28         if(r==0){
29             int flag =0;
30             for(ll i=2;i<=n;i++){
31                 if(i!=p)//起初if的{}写错了
32                 {                    
33                 printf("%lld 1\n",i);
34                 flag =1;
35                 break;
36                 }
37             }
38             if(!flag)printf("-1 -1\n"); //n=p=2
39         }
40         else{
41             ll ans=1ll;
42             for(ll i=2;i<=n;i++) {
43                 if(i!=p) ans =ans*i%p;
44             }
45             int  flag =0;
46             for(ll i=1;i<p;i++){
47                 if(ans*i%p==r) {
48                     printf("%lld %lld\n",p,i);
49                     flag =1;
50                     break;
51                 }
52             }
53             if(flag==0) printf("-1 -1\n");
54         }
55     }
56     else{
57         ll ans =1ll;
58         for(ll i=2;i<=n;i++) ans =ans*i%p;
59         int  flag =0;
60         ans = poww(ans,p-2);//放到里面会超时
61         for(ll i=2;i<=n;i++){
62             ll x= r*i%p*ans%p;
63             // ans/i*x%p==r
64             // x=r*i%p/ans%p;
65             if(x>=1&&x<i){
66                 printf("%lld %lld\n",i,x);
67                 flag =1;
68                 break;
69             }
70         }
71         if(!flag) printf("-1 -1\n");
72     }
73     return 0;
74     
75 }