LUOGU P2675 《瞿葩的数字游戏》T3-三角圣地
2024-10-07 23:10:25
解题思路
手推可以得出,最后每个数字的贡献其实就是第n行杨辉三角数,然后直接卢卡斯直接算(今天才找到lucas定理时间复杂度是log n,log以模数为底)。代码略麻烦,不想改了。
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#define LL long long
using namespace std;
const int MAXN = 1000005;
const int mod = 10007;
int n,num;
LL inv[MAXN],to[MAXN];
LL ans;
inline LL lucas(int n,int m){
if(n<m) return 0;
if(n<mod && m<mod) return to[n]*inv[m]%mod*inv[n-m]%mod;
return lucas(n%mod,m%mod)*lucas(n/mod,m/mod)%mod;
}
int main(){
scanf("%d",&n);
if(n==0) {puts("0");return 0;}
if(n==1) {puts("1");return 1;}
to[1]=1;
for(register int i=2;i<=n;i++)
to[i]=to[i-1]*i%mod;
inv[mod-1]=mod-1;
for(register int i=mod-2;~i;i--)
inv[i]=inv[i+1]*(i+1)%mod;
num=2;
ans=3;
for(register int i=1;i<=(n/2)+1;i++){
LL k=lucas(n-1,i);
ans=(ans+(++num)*k%mod)%mod;
if(num==n) break;
ans=(ans+(++num)*k%mod)%mod;
if(num==n) break;
}
printf("%lld",ans);
return 0;
}
最新文章
- iOS-屏幕适配-UI布局
- centos7 firewall 防火墙
- ORA-01501: CREATE DATABASE failed
- Entity Framework 之Database first(数据库优先)&;Model First(模型优先)
- 混合高斯模型和EM
- ShortestPath:Six Degrees of Cowvin Bacon(POJ 2139)
- .net 后台设置meta的属性(keywords,description)
- 在Android中使用并发来提高速度和性能
- java jvm学习笔记七(jar包的代码认证和签名)
- android颜色对应的xml配置值,颜色表
- JavaScript高级程序设计17.pdf
- ORACLE 热备begin backup / end backup
- eclipse 发布web工程,修改tomcat端口
- BZOJ 3512: DZY Loves Math IV [杜教筛]
- Python_语法和界面设计
- 痞子衡嵌入式:ARM Cortex-M内核MCU开发那些事 - 索引
- mysql8.0 Server 在Windows平台中的安装、初始化和远程访问设置
- Python if __name__ == '__main__':(以主程序形式执行)
- codeforces 982A Row
- C++ 方阵原地旋转90度