Watching movies (movie): the number of combinations

Description

To a rare vacation, white class organization go to the movies. However, due to the holiday movie too many people, it is difficult to do with the whole class took a fancy to a movie, we finally found a movie theater in a remote small alley. But this way of allocating seats cinema is very special in the following way:

  1. A total of K seat in the theater, and was numbered 1 ... K, everyone After buying tickets will be randomly assigned a seat, specifically 1 ... K medium may be randomly selected from a positive integer, which is set to L. from
  2. L If the number of seats are empty, then the seat will be assigned to this person, otherwise L plus one, to continue the previous steps.
  3. L If the number of seats does not exist in a second step, the person can only stand and watch a movie, the so-called standing tickets.

A total of N class white people (including myself white), as a lover of mathematics, White would like to know the whole class can have a seat probability is.

 

Input

The first line of the input file has only a positive integer and T, represents a group of test data.
Of 2 ~ T + 1 lines, each two positive integers N, K, separated by a single space, the same meaning described the subject.

Output

Output file contains T lines.
The i-th line should contain two space-separated integers A, B, represents the answer input file i-th group of data is A / B. (Note that this requirement will answer into irreducible fraction)

Sample Input

3
1 1
2 1
2 2

Sample Output

1 1
0 1
3 4

Hint

To 100% of the data T <= 50, N, K <200 is = ,. 1 <= n-<= 101 810 ^ 18 is {} . 1 0 . 1 . 8 ,. 1 <= Q <= 10510 ^. 5 . 1 0 5

I worked out a formula to talk about the spiritual path, tell it the normal way, free to skip

No clue, no formula to push, how to do?

Mold small point play table ah (not enough time did not have time to play in the code room to play table, so Shoumo super class bitterness)

The denominator is obvious, k the n- , looking for molecules:

Section 4 of the results of cultural studies, and then a big point really does not die out, including the 55

N = k where the first observation may be found to be (n-+. 1) n--. 1

Guessing at the question mark value is 6 . 4 = 1296

Line 1, item by item to make the difference: 11111

Line 2, is: 579. One more time: 22.

Line 3, calculating the difference between: 3458. Again: 24.

Found that the poor do several times, it will be a constant column. N is the number of times to make the difference. This sequence is proved n-1 linear function.

Then found: In each biased by a diagonal line 45 of the right observation, f (n, k) will be (k-n + 1) is divisible. After a number divisible by all business power.

Law Found: molecule is a (n-K-+. 1) × (K +. 1) n--. 1

Next, consider about minutes, (k + 1) k and irreducible denominator on the apparent molecular, only k-n + 1 requires about minutes to k

But it may be more than about once! The k = 18 and n = 15, are then carried out for about 18 minutes and 4

The remaining 2 and 9, do not worry to high molecular denominator, because we can come up with another k = 18, about 2 to clean.

So in addition to constantly, until the number is not enough or gcd of k 1.

Normal ideas:

After the launch of the equation, I began to consider what is the meaning of this formula:

We've added a seat, representing the people dry out, and finally just procuratorial this position no one can.

As we put all together into a ring, you can calculate the number of legal program.

 1 #include<cstdio>
 2 struct gj{
 3     long long x[40];int ws;
 4      friend void operator*=(gj &a,int t){
 5          for(int i=0;i<=a.ws;++i)a.x[i]*=t;
 6          for(int i=0;i<=a.ws;++i)a.x[i+1]+=a.x[i]/1000000000000000,a.x[i]%=1000000000000000;
 7          if(a.x[a.ws+1])a.ws=a.ws+1;
 8      }
 9      void print(){
10          printf("%lld",x[ws]);x[ws]=0;
11          for(int i=ws-1;i>=0;--i)printf("%015lld",x[i]),x[i]=0;
12          ws=0;x[0]=1;
13      }
14 }fz,fm;
15 int gcd(int a,int b){return b?gcd(b,a%b):a;}
16 int k,n,t;
17 int main(){
18     scanf("%d",&t);fz.x[0]=fm.x[0]=1;
19     while(t--){
20         scanf("%d%d",&n,&k);
21         if(n>k){puts("0 1");continue;}
22         int ex=k-n+1,tms=n;
23         while(gcd(ex,k)!=1&&tms)fm*=k/gcd(ex,k),ex/=gcd(ex,k),tms--;
24         fz*=ex;
25         for(int i=n-1;i;i--)fz*=k+1;
26         for(int i=tms;i;--i)fm*=k;
27         fz.print();putchar(' ');fm.print();puts("");
28     }
29 }
For details, see the code

Guess you like

Origin www.cnblogs.com/hzoi-DeepinC/p/11131615.html