【Lucas组合数定理+中国剩余定理】Mysterious For-HDU 4373

Mysterious For-HDU 4373 题目描述

MatRush is an ACMer from ZJUT, and he always love to create some special programs. Here we will talk about one of his recent inventions.

This special program was called "Mysterious For", it was written in C++ language, and contain several simple for-loop instructions as many other programs. As an ACMer, you will often write some for-loop instructions like which is listed below when you are taking an ACM contest.
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
for (int k = j; k < n; k++) {
blahblahblah();
}
}
}

Now, MatRush has designed m for-loop instructions in the "Mysterious For" program, and each for-loop variable was stored in an array a[], whose length is m.

The variable i represents a for-loop instructions is the i-th instruction of the "Mysterious For" program.There only two type of for-loop instructions will occur in MatRush's "Mysterious For" program:

1-type: if a for-loop belongs to 1-type, it will be an instruction like this:
for (int a[i] = 0; a[i] < n; a[i]++) {
...
}

2-type: if a for-loop belongs to 2-type, it will be an instruction like this:
for (int a[i] = a[i - 1]; a[i] < n; a[i]++) {
...
}

In addition, after the deepest for-loop instruction there will be a function called HopeYouCanACIt(), here is what's inside:
void HopeYouCanACIt() {
puts("Bazinga!");
}

So, the "Mysterious For" program, obviously, will only print some line of the saying: "Bazinga!", as it designed for.

For example, we can assume that n equals to 3, and if the program has three 1-type for-loop instructions, then it will run 3 3=27 times of the function HopeYouCanACIt(), so you will get 27 "Bazinga!" in total. But if the program has one 1-type for-loop instruction followed by two 2-type for-loop instructions, then it will run 3+2+1+2+1+1=10 times of that function, so there will be 10 "Bazinga!" on the screen.

Now MatRush has the loop length n and m loop instructions with certain type, then he want to know how many "Bazinga!" will appear on the screen, can you help him? The answer is too big sometimes, so you just only to tell him the answer mod his QQ number:364875103.

All for-loop instructions are surely nested. Besides, MatRush guaranteed that the first one belongs to the 1-type. That is to say, you can make sure that this program is always valid and finite. There are at most 15 1-type for-loop instructions in each program.
Input
First, there is an integer T(T<=50), the number of test cases.
For every case, there are 2 lines.
The first line is two integer n(1<=n<=1000000) and m(1<=m<=100000) as described above.
The second line first comes an integer k(1<=k<=15), represents the number of 1-type loop instructions, then follows k distinctive numbers, each number is the i-th 1-type loop instruction's index(started from 0), you can assume the first one of this k numbers is 0 and all numbers are ascending.
All none 1-type loop instructions of these m one belongs to 2-type.

题目大意

给你循环的类型,和循环的个数,求能循环几次。

分析

扩展Lucas定理+中国剩余定理:大意就是你在mod的p是一个合数,那么就把他拆成p1*p2,两个质数的乘机。
我们可以发现第一类循环最多就只有15个,我的思路的突破口就是在这个15。
我们把这些循环分成一个一个的区块,每一个区块的第一个循环就是第一类循环,那么就是要求出每个循环块的答案在相乘就可以了。
那么我们看到每一个循环块,它的本质就是要取出x个数,但是每一个数都是可以相同的。(注:和每一个数都不一样的不一样,不一样的话答案就非常简单是 \(C^x_n\)
第一重循环就是n次,第二次循环后面的循环都是第二层循环,那么第二重循环就是\(C^2_{n}+n\),以此类推,那么第三重循环就是$ C^3_{n+2} $,可以理解成在末尾在放两个数,但是所有的数都不能相同。
第三层的推导:

【Lucas组合数定理+中国剩余定理】Mysterious For-HDU 4373

那么我们就得到了结论:第m层循环的次数为\(C^m_{n+m-1}\)
但是由于q不是一个质数,而是两个质数的乘机,所以先用卢卡斯分别求出余数的乘机,再列出等式发现是一个同余方程,就可以使用中国剩余定理或者费马小定理算求解答案。

AC代码 #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include <queue> #include <cctype> #include <cmath> #include <time.h> #include <map> #include <set> #include <vector> using namespace std; #define ms(a,b) memset(a,b,sizeof(a)) typedef long long ll; const int maxn=1000003; const int maxm=100003; const int mod=364875103; const int mod1=97; const int mod2=3761599; int n,m; ll p1[mod1+10],p2[mod2+10]; void init(){ p1[0]=1,p2[0]=1; for (int i=1;i<=mod2;i++) p2[i]=p2[i-1]*i%mod2; for (int i=1;i<=mod1;i++) p1[i]=p1[i-1]*i%mod1; } inline int read(){ int X=0,w=0; char ch=0; while(!isdigit(ch)) {w|=ch=='-';ch=getchar();} while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar(); return w?-X:X; } ll inv(ll x,ll y,ll mod) { ll ret=1; while (y) { if (y&1) ret=ret*x%mod; x=x*x %mod; y>>=1; } return ret; } ll calc1(int n,int m) { if (n<m) return 0; ll x1=p1[n],x2=p1[m],x3=p1[n-m]; return x1*inv(x2,mod1-2,mod1)%mod1*inv(x3,mod1-2,mod1)%mod1; } ll calc2(int n,int m) { if (n<m) return 0; ll x1=p2[n],x2=p2[m],x3=p2[n-m]; return x1*inv(x2,mod2-2,mod2)%mod2*inv(x3,mod2-2,mod2)%mod2; } ll lucas2(int n,int m) { if (n<m) return 0; if (m==0) return 1; return calc2(n%mod2,m%mod2)*lucas2(n/mod2,m/mod2)%mod2; } ll lucas1(int n,int m) { if (n<m) return 0; if (m==0) return 1; return calc1(n%mod1,m%mod1)*lucas1(n/mod1,m/mod1)%mod1; } ll chinese(ll a[]) { ll x1=inv(mod1,mod2-2,mod2)*a[1]%mod*mod1%mod; ll x2=inv(mod2,mod1-2,mod1)*a[0]%mod*mod2%mod; return (x1+x2)%mod; } ll solve(int n,int m) { ll ans[2]; ans[0]=lucas1(n,m); ans[1]=lucas2(n,m); return chinese(ans); } int main() { int cas=read(); init(); for (int t=1;t<=cas;t++) { n=read(),m=read(); int k=read(),x=read(); ll ans=1; for (int i=1;i<k;i++) { int y=read(); ans=ans*solve(n+y-x-1,y-x)%mod; x=y; } ans=ans*solve(n+m-x-1,m-x)%mod; printf("Case #%d: %lld\n",t,ans); } return 0; }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyjgwf.html