HDU5226 【LUCAS定理】

(⊙v⊙)嗯。其实这题我是看了题解才开始做的。然后又DEBUG了几个小时。。。。

发现以前居然没有实际意义上的DEBUG过。!!真的是惭愧。

现在终于明白对着一个WA掉得代码debug上几个小时真的是!!!多么的有意义!!!!【摔!】

第一是感觉自己真的真的想问题不是很周到。所以我这种就是高中的时候做那种“1. a<0 0<b<x 2.a<0 b....."经常漏情况那种人。。或者就是。。。看到一道题。。啊!不就是这样嘛!然后刷刷刷。。之后发现旁边的人都写得老长老长。。。。。

第二是DEBUG需要方向。捋一捋DEBUG的思路。错了就应该先思考是不是思路错了,觉得没错的话就看看是不是哪里代码没有处理好。手动举几个例子(小数据) 看看中间过程哪里错了。还不行就和AC程序对拍,看看大数据上有没有问题。我是在这里卡了。刚开始是大数据一测就有问题。于是就看中间过程有没有错。后来发现是思路错了。改了之后发现大数据都过了还是不对。于是,在我说完”我测得数据都是对的!“之后。。。。随手输入一个小数据就错掉了。。。秒速打脸有没有。。其实应该想到的,大数据都对了那一定是边界错了。

思路题解上讲的挺清楚的了,不多说。

其实看到题目的时候。。满心想的是。。数论啊不做不做!!

Tom and matrix

Problem Description

Tom was on the way home from school. He saw a matrix in the sky. He found that if we numbered rows and columns of the matrix from 0, then,  ai,j=Cji

if i < j,  ai,j=0

Tom suddenly had an idea. He wanted to know the sum of the numbers in some rectangles. Tom needed to go home quickly, so he wouldn't solve this problem by himself. Now he wants you to help him. Because the number may be very large, output the answer to the problem modulo a prime p.

Input

Multi test cases(about 8). Each case occupies only one line, contains five integers,  x1y1x2y2p.x1x2105,y1y2105,2p109 .

You should calculate  x2i=x1y2j=y1ai,j  mod p

Output

For each case, print one line, the answer to the problem modulo p.

Sample Input
0 0 1 1 7
1 1 2 2 13
1 0 2 1 2
Sample Output
3
4
1

代码:

#include <stdio.h>
#include <iostream>
using namespace std;
#define maxn 101000
#define ll long long
ll A[maxn],inv[maxn];
ll mod;
ll Pow(ll a,ll b)
{
    ll ret=1;
    a%=mod;
    while(b)
    {
        if(b&1) ret=ret*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return ret%mod;
}
void init()
{
    A[0]=1,A[1]=1;
    inv[1]=1;inv[0]=1;
    for(int i=2;i<maxn;i++)
    {A[i]=A[i-1]*(ll)i%mod;inv[i]=Pow(A[i],mod-2);}
}

ll C(ll a,ll b)
{
    if(a<b) return 0;
    if(a<mod&&b<mod) return (A[a]*inv[b]%mod)*inv[a-b]%mod;
    return C(a/mod,b/mod)*C(a%mod,b%mod)%mod;
}
int main()
{
    ll x,xx,y,yy;
    while(scanf("%lld%lld%lld%lld%lld",&x,&y,&xx,&yy,&mod)!=EOF)
    {
        init();
        ll ans=0;
        if(y>x&&yy>xx&&y>xx) {printf("0\n");continue;}
        if(y>x) x=y;
        if(yy>xx) yy=xx;
        ll st=x,en=xx,h=y;
        while(h<=yy&&st<=en)
        {
            if(st<h) st=h;
            ans=(ans+C(en+1,h+1)-C(st,h+1)+mod)%mod;
            h++;
        }
        printf("%lld\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值