HDU 5794(杨辉三角+Lucas)

本文介绍了一种计算棋盘上特定棋子避开障碍到达指定位置的不同路径数量的方法。利用杨辉三角特性及Lucas算法优化计算过程,在面对大规模棋盘时仍能高效得出答案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A Simple Chess

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1784    Accepted Submission(s): 477


Problem Description
There is a n×m board, a chess want to go to the position
(n,m) from the position (1,1).
The chess is able to go to position (x2,y2) from the position (x1,y1), only and if only x1,y1,x2,y2 is satisfied that (x2x1)2+(y2y1)2=5, x2>x1, y2>y1.
Unfortunately, there are some obstacles on the board. And the chess never can stay on the grid where has a obstacle.
I want you to tell me, There are how may ways the chess can achieve its goal.
 

Input
The input consists of multiple test cases.
For each test case:
The first line is three integers, n,m,r,(1n,m1018,0r100), denoting the height of the board, the weight of the board, and the number of the obstacles on the board.
Then follow r lines, each lines have two integers, x,y(1xn,1ym), denoting the position of the obstacles. please note there aren't never a obstacles at position (1,1).
 

Output
For each test case,output a single line "Case #x: y", where x is the case number, starting from 1. And y is the answer after module 110119.
 

Sample Input
1 1 0 3 3 0 4 4 1 2 1 4 4 1 3 2 7 10 2 1 2 7 1
 

Sample Output
Case #1: 1 Case #2: 0 Case #3: 2 Case #4: 1 Case #5: 5


题目大意:

一个棋盘中有一个棋子在(1,1)点,棋子只能走日字型并且只能向右下方走,并且棋盘上有x个障碍点,棋子不能落在障碍点上,问走到(n,m)点有多少种方法。

解题思路:

先考虑没有障碍的情况:棋子从(1,1)到(3,2)与(2,3)只有一种走法,到(5,3),(3,5)有一种走法,到(4,4)有两种走法。

(5,3)只能由(3,2)到达,(3,5)只能由(2,3)到达,(4,4)这个点是由(3,2)与(2,3)这两点到达的,因此走的方法为(3,2)与(2,3)两点方法的和。

由此递推,每个点到达的方法数是由上面两个可到达点的方数的和。因此是一个杨辉三角。

                  1

            1           1

      1           2           1

1          3             3          1

............................................

同时注意到题目中n,m的范围是10^18,因此无法通过开数组递推得到每个点的方法数。

这时候就想到杨辉三角每个点的公式为C(n,m)(n为层数,m为第几个)

但是直接计算会导致数值过大,因此就要用到Lucas算法,之后就能得到结果。

之后考虑障碍物的情况:

每个在棋子能到达点上的障碍物都会对后面的结果产生影响,因此要先考虑每个障碍物对后面的点的影响。

每个上层的障碍物在下层的每个点中出现的次数又是一个以该点为顶点的小杨辉三角,因此去掉上层的点(n,m)对下层的点(N,M)的影响为:C(N,M)-C(N-n,M-m)*C(n,m)。(若M-m小于0则代表上层的点不会对下层该点产生影响)

将所有障碍物按照在杨辉三角中的层数进行排序,层数小的在前面。

之后遍历所有的障碍点,对每个后面层能产生障碍的点进行一次计算,把后面障碍点的方法数重新计算。

因为障碍点数量为100因此O(n^2)的复杂度可行。

然后再遍历每个障碍点对终点进行一次计算。

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<bitset>
#include<queue>
#include<stack>
#include<list>
#include<set>
#include<math.h>
#include<map>
using namespace std;
long long F[110200];
map<long long,map<long long,int>>vis;
struct point
{
    long long x,y,num,num2;
    point():num2(0){}
};
point pp[105];
bool cmp(point a,point b)
{
    if(a.x<b.x)return true;
    if(a.x==b.x&&a.y<=b.y)return true;
    return false;
}
void init(long long p)
{
    F[0] = 1;
    for(long long i = 1;i < 110200;i++)
        F[i] = F[i-1]*i%p;
}
long long inv(long long a,long long m)
{
    if(a == 1)return 1;
    return inv(m%a,m)*(m-m/a)%m;
}
long long Lucas(long long n,long long m,long long p)
{
    long long ans = 1;
    while(n&&m)
    {
        long long a = n%p;
        long long b = m%p;
        if(a < b)return 0;
        ans = ans*F[a]%p*inv(F[b]*F[a-b]%p,p)%p;
        n /= p;
        m /= p;
    }
    return ans;
}
bool judge(long long n,long long m)//判断该点是否在杨辉三角上,如果不在直接输出0
{
    if((n+m)%3!=0)return false;
    else 
    {
        long long hang=(n+m)/3;
        if((n-hang)<0||(n-hang)>hang)return false;
        return true;
    }
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    long long n,m,p,k;
    int t=1;
    p=110119;
    init(p);
    while(scanf("%lld%lld%lld",&n,&m,&k)!=EOF)
    {
        vis.clear();
        int len=0,flag=0;
        while(k--)
        {
            long long x,y;
            scanf("%lld%lld",&x,&y);
            if((x==1&&y==1)||(x==n&&y==m))
                flag=1;
            if(judge(x-1,y-1))
            {
            if(vis[x][y]==0)
            {
                pp[len].x=x;
                pp[len].y=y;
                pp[len++].num=Lucas((x+y-2)/3,x-(x+y-2)/3-1,p);
                vis[x][y]=1;
            }
            }
        }
        sort(pp,pp+len,cmp);
        for(int i=0;i<len;i++)
            for(int j=i+1;j<len;j++)
            {
                if((pp[j].x>pp[i].x)&&(pp[j].y>pp[i].y))
                {
                long long xx=pp[j].x-pp[i].x+1,yy=pp[j].y-pp[i].y+1;
                if(judge(xx-1,yy-1))
                {
                    pp[j].num=(pp[j].num-(Lucas((xx+yy-2)/3,xx-(xx+yy-2)/3-1,p)*pp[i].num)%p+p)%p;
                }
                }
            }
        if(flag==1||!judge(n-1,m-1))
            printf("Case #%d: 0\n",t);
        else
        {
            long long hang=(n+m-2)/3;
            long long lie=n-hang-1;
            long long num=Lucas(hang,lie,p);
            for(int i=0;i<len;i++)
            {
                if((n>pp[i].x)&&(m>pp[i].y))
                {
                    long long xx=n-pp[i].x+1,yy=m-pp[i].y+1;
                    if(judge(xx-1,yy-1))
                    {
                        num=(num-(Lucas((xx+yy-2)/3,xx-(xx+yy-2)/3-1,p)*pp[i].num)%p+p)%p;
                    }
                }
            }
            printf("Case #%d: %lld\n",t,num);
        }
        t++;
    }
    return 0;
}


内容概要:本文针对国内加密货币市场预测研究较少的现状,采用BP神经网络构建了CCi30指数预测模型。研究选取2018年31日至2019年326日共391天的数据作为样本,通过“试凑法”确定最优隐结点数目,建立三层BP神经网络模型对CCi30指数收盘价进行预测。论文详细介绍了数据预处理、模型构建、训练及评估过程,包括数据归一化、特征工程、模型架构设计(如输入层、隐藏层、输出层)、模型编译与训练、模型评估(如RMSE、MAE计算)以及结果可视化。研究表明,该模型在短期内能较准确地预测指数变化趋势。此外,文章还讨论了隐层节点数的优化方法及其对预测性能的影响,并提出了若干改进建议,如引入更多技术指标、优化模型架构、尝试其他时序模型等。 适合人群:对加密货币市场预测感兴趣的研究人员、投资者及具备一定编程基础的数据分析师。 使用场景及目标:①为加密货币市场投资者提供一种新的预测工具和方法;②帮助研究人员理解BP神经网络在时间序列预测中的应用;③为后续研究提供改进方向,如数据增强、模型优化、特征工程等。 其他说明:尽管该模型在短期内表现出良好的预测性能,但仍存在一定局限性,如样本量较小、未考虑外部因素影响等。因此,在实际应用中需谨慎对待模型预测结果,并结合其他分析工具共同决策。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值