A disguised version of the wrong-line formula-Express delivery

Source: Internet
Author: User

Title: Express Small Brother every day is fortunate to send courier, today he needs to send n a courier to n a recipient, the first express need to send to the first recipient, I would like to ask which happened exactly K sent the wrong number of cases is how much?

Input: Multiple examples, two numbers per line N and K,1<<n<<1000,0<<k<<n, two are 0 end, the sample does not need to be processed.

Output: Each line outputs a result, because the values are larger, so all results require a 10^9+7 modulo.


Analysis: Water problem

1, using the wrong row formula to calculate exactly K of the number of rows of the wrong, pre-processing an array.

2, from the N medium remove K have C (n,k) species.

3, C (n,k) and f[k] are multiplied to the final result.


Code:

#include <iostream>using namespace Std;__int64 f[1005];void Init () {f[0]=0;f[1]=0;f[2]=1;for (int i=3;i<=1001 ; i++) f[i]= ((i-1) * ((F[i-1]+f[i-2])%1000000007)%1000000007;} __int64 CNK (int n,int k) {__int64 a,b;int i;a=b=1;for (i=k+1;i<=n;i++) a*=i;for (i=2;i<=n-k;i++) B*=i;return A/b;} int main () {int n,k;init (); while (cin>>n>>k && (n| | k) {printf ("%i64d\n", (CNK (n,k) *f[k])%1000000007);}    return 0;}


A disguised version of the wrong-line formula-Express delivery

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.