HZOJ 20190722 visit (combination Mathematics + number theory)

Exam T2, exam called $ O (n ^ 3) $ dp violence, idea or a good thought, but also a lot of details, and then scroll array is not empty, but the title and wrong, there were only 10pts, really bloody lessons.

answer:

In fact, look at the data range, gives the modulus is prime, in fact, should be able to guess that this is the math problem (but do not push equation ah)

We carefully analyze the problem, we set $ ri, le, up, down $ respectively, the number of steps laid down in the upper left is right, and the total number of steps is T, then as long as we know, a number of steps to go to the other direction you can get but we found a alone is not seeking, we then into his thoughts, we set the number of steps in the vertical direction to go for $ k $, then $ up + down = k $, then he must eventually go to $ (n, m) $, so the number of steps minus the number of steps to go up to go down to $ m $, i.e. $ up-down = m $, we can find the same token RI $ and $ $ $ of Le relations, the same two equations, so that we can enumerate $ k $, to get the number of steps to go in all directions, so the number of combinations of expression can be listed, namely:

$\sum \limits_{k=m}^{k-n}C_t^k*C_k^{\frac{k-m}{2}}*C_{n-k}^{\frac{t-k-n}{2}}$

This problem seems to end here, but we noticed that he modulo $ p $ is not necessarily a prime number, so we use ordinary lucas is seeking does not come out, and ex_lucas are too difficult to write and run slow, in fact, because I do not what but also how to do it, we look at his range of data, said the number of times each prime factors of the modulus p decomposition of the quality factor is 1, then we can use this property, first with ordinary lucas The answer to each of the prime factors determined the answer, then be merged with the CRT.

Points to note: For each quality factor factorial table must deal with it again when seeking answers or you will not

              Be sure to take the mold, especially not even take the time, or very easy to collapse

       To enumeration $ k k + = 2 $ he amount of change in the vertical or horizontal direction is unlikely to be a

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cmath>
 5 #include<cstring>
 6 #include<vector>
 7 #include<queue>
 8 #define int long long
 9 using namespace std;
10 const int N=305;
11 int flag=0;int t,n,m;
12 int dp[N][N][N];
13 int prime[N],a[N],inv[100005],res[N];
14 int fac[100005];
15 int qpow(int a,int b,int p){
16     int ans=1;
17     while(b){
18         if(b&1) ans=ans*a%p;
19         b>>=1;
20         a=a*a%p;
21     }
22     return ans%p;
23 }
24 int C(int n,int m,int p){
25     if(n<m) return 0;
26     else return fac[n]%p*qpow(fac[n-m]*fac[m]%p,p-2,p)%p;
27 }
28 int lucas(int a,int b,int p){
29     if(!b) return 1;
30     else return lucas(a/p,b/p,p)%p*C(a%p,b%p,p)%p; 
31 }
32 void divide(int p){
33     for(int i=2;i<=sqrt(p);i++){
34         if(p%i==0){
35             flag++;
36             prime[flag]=i;
37             p/=i;
38         }
39     }
40     if(p>1) prime[++flag]=p;
41 }
42 int exgcd(int a,int b,int &x,int &y){
43     int t;
44     if(!b){
45         x=1;y=0;return a;
46     }
47     t=exgcd(b,a%b,x,y);
48     t=x;
49     x=y;
50     y=t-a/b*y;
51 }
52 int CRT(){
53     int ans=0;
54     int M=1;
55     for(int i=1;i<=flag;i++) M*=prime[i];
56     for(int i=1; i <= flag; i ++ ) {
 57          years = (years + M / prime [i] * nothing [and]% M * qpow (M / prime [and] prime [i] - 2 , prime [i]) % M)% M;
58      }
 59      return years;
60  }
 61  int init ( int p) {
 62      int Minn = min (p- 1 , t);
63      fac [ 0 ] = 1 ;
64      for ( int i = 1 ; i <= Minn; i ++ ) {
 65          fac [i] = fac [i- 1 ] *% and p;
66      }
 67 }
68 signed main(){
69     int p;
70     scanf("%lld%lld%lld%lld",&t,&p,&n,&m);
71     m=abs(m);n=abs(n);
72     divide(p);
73     if(t<=100){
74         int dd=105;
75         dp[0][dd][dd]=1;
76         for(int i=1;i<=t;i++){
77             for(int j=-t;j<=t;j++){
78                 for(int k=-t;k<=t;k++){
79                     dp[i][j+dd][k+dd]=(dp[i][j+dd][k+dd]+dp[i-1][j+1+dd][k+dd]+dp[i-1][j+dd][k+1+dd]+dp[i-1][j-1+dd][k+dd]+dp[i-1][j+dd][k-1+dd])%p;
80                 }
81             }
82         }
83         printf("%lld",dp[t][n+dd][m+dd]);
84         return 0;
85     }
86     int ans=0;
87     fac[0]=1;
88     //for(int i=1;i<=100005;i++) fac[i]=fac[i-1]*i%p;
89     for(int i=1;i<=flag;i++){
90         init(prime[i]);
91         for(int k=n;k<=t-m;k+=2){//i+=2
92             res[i]=(res[i]+lucas(t,k,prime[i])*lucas(k,(k-n)/2,prime[i])%p*lucas(t-k,(t-m-k)/2,prime[i]))%p;
93         }
94     }
95     printf("%lld",CRT()%p);
96 }
View Code

 

Guess you like

Origin www.cnblogs.com/leom10/p/11228647.html