nefu 628 Garden visiting

题目:给出一个n*m大的花园,求出从左上角到右下角的路径数目(路径单调)。

方法:路径数=C(m+n-2,m-1);别忘了最后对p取余。由于数据最大能达到10^5,使用杨辉三角记录的话会爆内存,所以只能换方法。

由于C(x,y)=x!/(y!*(x-y)!),这里我们可以将x!分解素因子,并保存记录下来,同样的方法记录后面两个,由于x!必然能够整除(y!*(x-y)!),所以后面两个数有的因子,x!比然有,只需要将他们的因子的指数相加减,就能得到最后结果的素因子分解的情况,然后最后使用快速幂取模,就能得到最后的结果。

注意:如何进行素因子分解?

首先要打表将所有的素因子求出来,这里有是将n!进行素因子分解,假设想要求出其中有多少个5,这里是有技巧的。

假设n=200,那么因子5的个数=200/5+40/5+8/5=49,怎么得到的呢?200中5的倍数有40个,这40个数中其中是25的倍数的有8个,所以还能分解出8个5,这8个数中还有一个是125的倍数,还能分解出一个5,就这样一直循环下去,就能求出指数的值。

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int prim[17990];
int t[200001];
void init()            //打表求出所有的素数
{
    int k=0;
    memset(t,0,sizeof(t));
    int i,j;
    for(i=2; i<=200000; i++)
        if(!t[i])
        {
            prim[k++]=i;
            for(j=i+i; j<=200000; j+=i)
                t[j]=1;
        }
}
long long exp_mod(int a,int n,int b)    //快速幂取模
{
    long long t;
    if(n==0) return 1%b;
    if(n==1) return a%b;
    t=exp_mod(a,n/2,b);
    t=t*t%b;
    if((n&1)==1) t=t*a%b;
    return t;
}

int getsum(int n,int k)         //获得素因子的指数值
{
    int sum=0;
    while(n!=0)
    {
        sum+=n/k;
        n/=k;
    }
    return sum;
}

int main()
{
    int p,m,n;
    int t,i,j,k;
    init();
    cin>>t;
    while(t--)
    {
        cin>>m>>n>>p;
        if(n>m) swap(n,m);
        m=m+n-2;
        n--;

        int sum=0;
        long long ans=1;
        for(i=0; i<=17984&&prim[i]<=m; i++)
        {
            sum=getsum(m,prim[i])-getsum(n,prim[i])-getsum(m-n,prim[i]);
            if(sum!=0)
                ans=ans*exp_mod(prim[i],sum,p)%p;
            if(ans==0) break;
        }
        cout<<ans<<endl;
    }
    return 0;
}

nefu 628 Garden visiting

时间: 2024-08-06 19:50:49

nefu 628 Garden visiting的相关文章

NEFU 628 Garden visiting (数论)

Garden visiting Problem:628  Time Limit:1000ms  Memory Limit:65536K Description There is a very big garden at Raven's residence. We regard the garden as an n*m rectangle. Raven's house is at the top left corner, and the exit of the garden is at the b

nefu 628大组合数取模

题目连接:http://acm.nefu.edu.cn/JudgeOnline/problemshow.php?problem_id=628 关于ACM培训的通知 Garden visiting Problem : 628 Time Limit : 1000ms Memory Limit : 65536K description There is a very big garden at Raven's residence. We regard the garden as an n*m rect

数论poj题目

http://blog.sina.com.cn/s/blog_76f6777d0101ir50.html 1.素数,整数分解,欧拉函数 素数是可能数论里最永恒,最经典的问题了.素数的判断,筛法求素数,大素数的判断···还有很多其他问题都会用到素数. *最水最水的:(心情不爽时用来解闷吧) pku1365 Prime Land pku2034 Anti-prime Sequences pku2739 Sum of Consecutive Prime Numbers pku3518 Prime Ga

[转] POJ数学问题

转自:http://blog.sina.com.cn/s/blog_6635898a0100magq.html 1.burnside定理,polya计数法 这个大家可以看brudildi的<组合数学>,那本书的这一章写的很详细也很容易理解.最好能完全看懂了,理解了再去做题,不要只记个公式. *简单题:(直接用套公式就可以了) pku2409 Let it Bead      http://acm.pku.edu.cn/JudgeOnline/problem?id=2409 pku2154 Co

『转』数学专辑

1.burnside定理,polya计数法 这个大家可以看brudildi的<组合数学>,那本书的这一章写的很详细也很容易理解.最好能完全看懂了,理解了再去做题,不要只记个公式. *简单题:(直接用套公式就可以了) pku2409 Let it Bead   http://acm.pku.edu.cn/JudgeOnline/problem?id=2409 pku2154 Color http://acm.pku.edu.cn/JudgeOnline/problem?id=2154 pku12

ACM数学(转)

从放暑假前周sir给我讲了一个用polya计数法和burnside定理做的题目(pku2409)后,突然觉得组合数学挺有意思,然后从那时起到现在几乎都在做这类的题目. 做到现在感觉这类题目的一些基本知识点都差不多有所了解了,水题也刷了不少,但还有很多难题自己实在是做不动,所以准备把这类题目先放一放,然后把前段时间做的水题整理一下(供以后的初学者参考,大牛就不要看了哈,都是水题).剩下的比较难的题目就慢慢来吧,以后做出来再不上,这个小结会不断地更新.也希望大家有好的题目可以推荐一下,分享一下哈.

Codeforces Round #261 (Div. 2)459A. Pashmak and Garden(数学题)

题目链接:http://codeforces.com/problemset/problem/459/A A. Pashmak and Garden time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Pashmak has fallen in love with an attractive girl called Parmida s

Leetcode - 628 Maximum Product of Three Numbers

Leetcode - 628 Maximum Product of Three Numbers 628. Maximum Product of Three Numbers Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3] Output: 6 Example 2: Input: [1,2,3,4]

nefu 462 fib组合

nefu 462 fib组合 (斐波那契数列的通项公式以及推倒过程) 分类: 数学2014-05-21 10:27 190人阅读 评论(0) 收藏 举报 题目链接:http://acm.nefu.edu.cn/JudgeOnline/problemshow.php?problem_id=462 斐波那契数列的通项公式 推倒过程: 对于本题分析: 最后一行的一个变形为(6-2√5)^2/4 代码 [cpp] view plaincopyprint? #include <iostream> usi