Hnu 13101 The Triangle division of the convex Polygon Cattleya number nth%m (M can be non-prime

Source: Internet
Author: User
Tags mul

Topic Link: Click to open the link

First you have to n-=2, and then there is a Cattleya number.

The previous question was H (n) = h (n-1) * (4n-2)/(n+1);

The problem is h (n) = (2n)! * n! /(n+1)!;

Then the factorial decomposition factorization:

Click to open link

The direct fast power is broken down.

#include <stdio.h> #include <iostream> #include <cmath> #include <cstring>using namespace std;# define int __int64const int N = 100000;int prime[n],primenum;//has primenum prime math.hvoid prime (int max_prime) {primenum=0; prime[primenum++]=2;for (int i=3;i<=max_prime;i+=2) for (int j=0;j<primenum;j++) if (i%prime[j]==0) Break;else if (PRIME[J]&GT;SQRT (double) i) | | j==primenum-1) {prime[primenum++]=i;break;}}    int N, m;void mul (int &x, int y) {x *= y;    if (x >= m) x%= m; else if (x<0) x = x%m+m;}    int Pow (int x, int y) {int ans = 1;        while (y) {if (y&1) mul (ans, x);        Mul (x, x);    Y >>= 1; } return ans;        int d[n];void work1 (int x) {for (int i = 0; prime[i]<=x; i++) {d[i] = 0;    for (int j = prime[i]; j <= x; j*=prime[i]) d[i] + = x/j;            }}void work2 (int x) {for (int i = 0, prime[i]<=x; i++) for (int j = prime[i]; j <= x; J*=prime[i]) D[i]-= x/j;} void Debug () {for(int i = 0; i<primenum; i++) if (D[i]) printf ("[%d,%d]\n", Prime[i],d[i]);p UTS ("");}    int work () {if (m==1) return 0;if (n==1) return 1;int res = 1;    F[n] = 2n!/n!/(n+1)!;    Work1 (2*n);    WORK2 (n);    WORK2 (n+1);    for (int i = 0; prime[i] <= 2*n; i++) if (D[i]) mul (res, Pow (Prime[i],d[i])); return res;} #undef Intint Main () {PRIME (1000000), while (cin>>n>>m) {n-=2;cout<< work ()%m <<endl;} return 0;}



Hnu 13101 The Triangle division of the convex Polygon Cattleya number nth%m (M can be non-prime

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.