题意:

定义函数Concatenate (1 ..N)是将所有正整数 1, 2, …, N 顺序连接起来得到的数,如concatenate(1..5)是12345,求concatenate(1...n)%m的值

思路:

矩阵快速幂,公式为

$$
\left[
\begin{matrix}
f(n)\\n\\1
\end{matrix}
\right]=
\left[
\begin{matrix}
10^k&1&1\\
0&1&1\\
0&0&1
\end{matrix}
\right]
\left[
\begin{matrix}
f(n-1)\\n-1\\1
\end{matrix}
\right]
$$

其中$10^k$可以分快处理,分n为0~9,10~99,100~999等

代码:

/**************************************************************
Problem: 2326
User: wrjlinkkkkkk
Language: C++
Result: Accepted
Time:64 ms
Memory:1292 kb
****************************************************************/ #include<iostream>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map>
#include<functional>
#include<list> #define fst first
#define sc second
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lc root<<1
#define rc root<<1|1
#define lowbit(x) ((x)&(-x))
#pragma Gcc optimize(2) using namespace std; typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL; const int maxn = + ;
const int maxm = 5e3 + ;
const double eps = 1e-;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
int scan(){
int res=,ch,flag=;
if((ch=getchar())=='-')
flag=;
else if(ch>=''&&ch<='')
res=ch-'';
while((ch=getchar())>=''&&ch<='')
res=res*+ch-'';
return flag?-res:res;
} ll n, m; ll mul(ll x,ll y){
ll s=;
while(y)
{
if(y&)s=(s+x)%m;
x=(x<<)%m;
y>>=;
}
return s;
}
void mtpl(ll a[][], ll b[][],ll s[][]){
ll tmp[][];
for(int i = ; i <= ; i++){
for(int j = ; j <= ; j++){
tmp[i][j]=;
for(int k = ; k <= ; k++){
tmp[i][j] += mul(a[i][k] , b[k][j])%m;
} } }
for(int i = ; i <= ; i++){
for(int j = ; j <= ; j++){
s[i][j] = tmp[i][j];
}
}
return;
}
ll b[][];
void Fast_power(ll t,ll last){
ll a[][];
mem(a, );
a[][] = t;
a[][] = a[][] = a[][] = a[][] = a[][] = ;
ll y = last-t/+;
while(y){
if(y & ) mtpl(b, a, b);
mtpl(a, a, a);
y >>= ;
}
//prt(ans);
return;
}
int main(){ scanf("%lld %lld", &n, &m); mem(b, );
for(int i = ; i <= ; i++)b[i][i] = ;
ll t = ;
while(t <= n){
Fast_power(t, t-);
t*=; }
Fast_power(t,n);
printf("%lld", b[][]%m);
return ;
}
/*
8 3287492749272074
*/

最新文章

  1. TCMalloc:线程缓冲的Malloc
  2. OWIN的理解和实践(一) – 解耦,协作和开放
  3. 使用升级版的 Bootstrap typeahead v1.2.2
  4. Nginx SPDY Pagespeed模块编译——加速网站载入
  5. 基础数据结构 之 栈(python实现)
  6. jQuery实例-简单选项卡-【一些常见方法(2)-练习】
  7. jQuery的类数组对象结构
  8. 退货行RMA编号改为必输选项
  9. .net下灰度模式图像
  10. 记一次DG搭建过程中ORA-09925: Unable to createaudit trail file 错误
  11. 关于kali linux 2.0的vmware tools的安装问题
  12. python默认参数陷阱
  13. 00.pt-toolkit 目录
  14. No such property: FOR_RUNTIME for class: org.gradle.api.attributes.Usage
  15. SQL Server 调用 C# 方法实现正则表达式验证
  16. 给linux系统配置网络
  17. Java日志输出问题
  18. 使用EventLog Analyzer监控、管理及分析日志
  19. &#39;org.springframework.web.filter.CharacterEncodingFilter&#39; is not assignable to &#39;javax.servlet.Filter,This inspection lets you spot the following problems that might occur in descriptors that are used t
  20. shell 后台执行脚本

热门文章

  1. mongodb 更新嵌套数组的值
  2. Docker+Nginx使用流程(笔记)
  3. linux下安装mysql5.7.25详细教程
  4. 【转】Spring面试问题集锦
  5. String类方法的使用
  6. HTTPS中的TLS
  7. 【Linux】---Linux系统下各种常用命令总结
  8. 解决 VS Code 中 golang.org 被墙导致的 Go 插件安装失败问题
  9. 基于Java+HttpClient+TestNG的接口自动化测试框架(四)-------参数存取处理
  10. 频繁插入(insert)的业务,用什么存储引擎更合适? | 数据库系列(转)