【Codeforces711E】ZS and The Birthday Paradox [数论]
ZS and The Birthday Paradox
Time Limit: 20 Sec Memory Limit: 512 MBDescription
Input
Output
Sample Input
4 3
Sample Output
23 128
HINT
Solution
Code
1 #include<iostream> 2 #include<string> 3 #include<algorithm> 4 #include<cstdio> 5 #include<cstring> 6 #include<cstdlib> 7 #include<cmath> 8 #include<vector> 9 using namespace std; 10 typedef long long s64; 11 12 const int ONE = 1000005; 13 const int MOD = 1e6 + 3; 14 15 s64 n, k; 16 s64 num = 0; 17 s64 Fz = 1, Fm = 1; 18 19 int get() 20 { 21 int res;char c; 22 while( (c=getchar())<48 || c>57 ); 23 res=c-48; 24 while( (c=getchar())>=48 && c<=57 ) 25 res=res*10+c-48; 26 return res; 27 } 28 29 s64 Quickpow(s64 a, s64 b) 30 { 31 s64 res = 1; 32 while(b) 33 { 34 if(b & 1) res = res * a % MOD; 35 a = a * a % MOD; 36 b >>= 1; 37 } 38 return res; 39 } 40 41 int main() 42 { 43 cin>>n>>k; 44 45 num = 1; 46 while((1LL << num) < k) num++; 47 if(num > n) {printf("1 1"); return 0;} 48 49 num = 0; 50 for(s64 i = k - 1; i >= 1; i >>= 1) 51 num += (i >> 1); 52 53 s64 a = Quickpow(2, n); 54 55 Fz = 1; 56 for(int i = 1; i < k; i++) 57 { 58 Fz = (s64)Fz * (a - i + MOD) % MOD; 59 if(Fz == 0) break; 60 } 61 Fm = Quickpow(a, k - 1); 62 63 int inv = Quickpow(Quickpow(2, num), MOD - 2); 64 65 Fz = (s64)Fz * inv % MOD, Fm = (s64)Fm * inv % MOD; 66 Fz = (Fm - Fz + MOD) % MOD; 67 68 69 cout<<Fz<<" "<<Fm<<endl; 70 }