HDU 3037 Saving Beans combination number template title.

Source: Internet
Author: User

Saving BeansTime limit:6000/3000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2707 Accepted Submission (s): 1014


Problem Descriptionalthough Winter is far away, squirrels has to work day and night to save beans. They need plenty of food to get through those long cold days. After some time, the squirrel family thinks that they has to solve a problem. They suppose that they would save beans in n different trees. However, since the food was not sufficient nowadays, they would get no more than M beans. They want to know, how many ways there is to save no more than M beans (they is the same) in n trees.

Now they turn-to-do, you should give them the answer. The result may be extremely huge; You should output the result modulo p, because squirrels can ' t recognize large numbers.
Inputthe first line contains one integer T, means the number of cases.

Then followed T lines, each line contains three integers n, m, p, means that squirrels would save no more than M same beans In n different trees, 1 <= N, M <= 1000000000, 1 < p < 100000 and p are guaranteed to be a prime.
Outputyou should output the answer modulo p.
Sample Input
21 2 52) 1 5

Sample Output
Hinthintfor Sample 1, squirrels'll put no more than 2 beans in one tree. Since trees is different, we can label them as 1, 2 ... and so on. The 3 ways are:put no beans, put 1 bean in tree 1 and put 2 beans in tree 1. For Sample 2, the 3 ways are:put no beans, put 1 beans in tree 1 and put 1 beans in tree 2.
One day, one day. It's not easy. Portal: http://blog.csdn.net/wukonwukon/article/details/7341270 combination number of modules need to be used, fast power to take the mold Portal: http://blog.csdn.net/ lsldd/article/details/5506933 Plate method: Http://blog.sina.com.cn/s/blog_7cc6f2770100red3.html Fermat theorem:/http baike.baidu.com/view/263807.htm where division takes modulo also requires inverse element. Code:
#include <stdio.h> #include <string.h> #include <iostream> #define SIZE 100010using namespace std; long  Long f[size]; long long modpow (long long A, long long n, long long p) {Long long ret = 1, a=a; while (n) {if (N & 1) ret = (ret*a)%p; A = (a*a)%p; n >>= 1;} return ret;} void fact (long long p) {f[0] = 1; for (int i = 1; I <= p; ++i) {f[i] = f[i-1]*i%p;}}  Long long A, long long a long B, long long p) {Long Long re = 1; while (a && b) {long long aa = a%p, BB = b%p; if (AA<BB) return 0; re = re*f[aa]*modpow (f[bb]*f[aa-bb]%p,p-2,p)%p; a/= p; b/= p;} return re;} int main () {int t; scanf ("%d", &t), while (t--) {Long long n, M, p; scanf ("%lld%lld%lld", &n,&m,&p); fact (P) ;p rintf ("%lld\n", Lucas (N+m,m,p)); return 0;}

with June

HDU 3037 Saving Beans combination number template title.

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.