计蒜客 贝壳找房计数比赛(可重全排列+逆元)

题目链接:https://nanti.jisuanke.com/t/27650

题目大意:

解题思路:

代码:

 1 #include<bits/stdc++.h>
 2 #define lc(a) (a<<1)
 3 #define rc(a) (a<<1|1)
 4 #define MID(a,b) ((a+b)>>1)
 5 #define fin(name)  freopen(name,"r",stdin)
 6 #define fout(name) freopen(name,"w",stdout)
 7 #define clr(arr,val) memset(arr,val,sizeof(arr))
 8 #define _for(i,start,end) for(int i=start;i<=end;i++)
 9 #define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
10 using namespace std;
11 typedef long long LL;
12 const int N=5e6+5;
13 const LL MOD=1e9+7; 
14 const int INF=0x3f3f3f3f;
15 const double eps=1e-10;
16 
17 LL mp[300];
18 
19 LL extend_gcd(LL a,LL b,LL &x,LL &y){
20     if(!b){
21         x=1;
22         y=0;
23         return a;
24     }
25     LL gcd=extend_gcd(b,a%b,x,y);
26     LL t=x;
27     x=y;
28     y=t-(a/b)*x;
29     return gcd;
30 }
31 
32 LL NY(LL num){
33     LL x,y;
34     extend_gcd(num,MOD,x,y);
35     return (x%MOD+MOD)%MOD;
36 }
37 
38 int main(){
39     FAST_IO;
40     int T;
41     cin>>T;
42     while(T--){
43         memset(mp,0,sizeof(mp));
44         string s,t;
45         cin>>s>>t;
46         for(int i=0;i<s.length();i++){
47             mp[s[i]]++;
48         }
49         bool flag=true;
50         for(int i=0;i<t.length();i++){
51             if(mp[t[i]]>0)
52                 mp[t[i]]--;
53             else
54                 flag=false;
55         }
56         if(!flag){
57             cout<<0<<endl;
58             continue;
59         }
60         LL ans=1;
61         LL len=s.length()-t.length();
62         for(int i=1;i<=len;i++){
63             ans=ans*i%MOD;
64         }
65         LL sum=1;
66         for(int i='a';i<='z';i++){    
67             for(int j=1;j<=mp[i];j++){
68                 sum=sum*j%MOD;
69             }
70         }
71         ans=ans*NY(sum)%MOD;
72         cout<<ans*(len+1)%MOD<<endl;
73     }
74     return 0;
75 }

猜你喜欢

转载自www.cnblogs.com/fu3638/p/9194017.html