Description

小 C 数学成绩优异,于是老师给小 C 留了一道非常难的数学作业题:

给定正整数 N 和 M,要求计算 Concatenate (1 .. N) Mod M 的值,其中 Concatenate (1 ..N)是将所有正整数 1, 2, …, N 顺序连接起来得到的数。例如,N = 13, Concatenate (1 .. N)=12345678910111213.小C 想了大半天终于意识到这是一道不可能手算出来的题目,于是他只好向你求助,希望你能编写一个程序帮他解决这个问题。

Input

从文件input.txt中读入数据,输入文件只有一行且为用空格隔开的两个正整数N和M,其中30%的数据满足1≤N≤1000000;100%的数据满足1≤N≤1018且1≤M≤109.

Output

输出文件 output.txt 仅包含一个非负整数,表示 Concatenate (1 .. N) Mod M 的值。

Sample Input

13 13

Sample Output

4

题解

首先写出递推关系式:令 $s_i$ 为前 i 个数连接得到的数,$c(i)$ 表示 $i$ 的位数,有

$s_i=10^{c(i)}×s_{i−1}+i$

$c(i)$ 随 $i$ 变化,无法在转移矩阵 $T$ 中表示。如果 $c(i)$ 固定,就可以矩阵优化了!

考虑位数随着 $i$ 的变化最多只会变化 $18$ 次,因此可以按位数分段进行矩阵快速幂。
枚举位数,那么有

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAcMAAAEqCAIAAADmr6zDAAAAA3NCSVQICAjb4U/gAAAgAElEQVR4Xu2defyXU/r/RbKFFlJJJSJS0qqislMGM2qylSVkmCkJTUrFkAZDjeVB2bK0DFG2RNpkqYShspTSQspSUilGfk/OfO/fez7v/d7e577v1/uPHp/u+5zrXNfrnPfrfc65rnOdcr/88st2+ggBISAEhIAHBLb3UFdVhYAQEAJC4FcExKQaB0JACAgBrwiISb0iqPpCQAgIATGpxoAQEAJCwCsCYlKvCKq+EBACQkBMqjEgBISAEPCKgJjUK4KqLwSEgBAQk2oMCAEhIAS8IiAm9Yqg6gsBISAExKQaA0JACAgBrwiISb0iqPpCQAgIATGpxoAQEAJCwCsCYlKvCKq+EBACQkBMqjEgBISAEPCKgJjUK4KqLwSEgBAQk2oMCAEhIAS8IiAm9Yqg6gsBIS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4wF9LBABP71r38x6ipXrrxixYoCqyShmJg0qr18/fXXM6D3228/tq7stOHxxx/fddddly9fnk29O++8ExPq1q2bscAf/vCH6667LuOr1IfXXHNNtWrVdt9994MOOghpZ555ZrYqX3zxBcWGDh2arYCeF4IAv0mdOnUC6nbt2vFjWUiVJJQRk0ayl995550ddtiB0Txx4kQ7DcApUatWrWuvvTaHeljx5JNPEr+Vscxxxx13xRVXZHyV+nDx4sUffPABs1f2i3MzKbVuvPHG3XbbLeMUOG9DKuAg8Nlnn/EbCdoPPfSQYDEIyOPEeIjYh5678sormQ40a9bstNNOs1N71tqrVq3Cz5tDvSOOOKJz585MbXKUyfuKfdjDDjuMLYK8JSlg/M733XdfIYVVJhsCderUufzyy3nbv39/lkTZiiXquZg0et39zDPP4IxGbxb4uJtKbsBXX3313HPPsZafPn06fxt9nn322QMOOACuzKYevwccJQj5NAE7ANBuaQMJsgESree47/n1wsV38803R0vzgLQtH5BciQ0IARxNLJkR3qhRI5w5BbYyfvx4eK158+Z5y+OQmTx58sKFC2moYcOG7IixHM5WC09uv379mH5Ci6YM3y42N/mavfbaa+eff34Ool+5ciVTG6wIOdKbUwwjRozAnVWpUqVsdgX9vCiQg1bGyC9WpRo1avTo0ePee+9ls/viiy9mZRCOnta2ojmptV2TWTECSD/99FPeDRw4cPvt83cfW1rdu3c/66yzJk2alFliytOnn366du3aL7zwQps2bdimxLcO2T366KMZKy5atKhBgwajR4/G5/Piiy8yTYZSDz30UL6Tb731Fv/uu+++GSuah3PnzuWPVq1a5SgTxCt2b/GZmEl9EPLzyiwK5LzSfCngTiV+0cuXL09HX3311b6oEW0h2jCOEAK4capUqcKAg8I4vZdNczwqTBW7det25JFH4tdmuFNlwIAB2cqb5yNHjqTY8OHDU4sR8sK8Mt3fTRNMcpmuvv3226nl0WrDhg2PPPIIotiOzNEi/EsZGs1WpkCPk1O9EI8ThZ944gna5QcpW7uBPi8K5EA1cYR7UemCCy4ATD5vvvlmONpa24pW92YkRONfyOLbb79FV1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SEQEgIiElDAlrNCAEhEGMExKQx7lyZJgSEQEgIiElDAlrNCAEhEGMExKQx7lyZJgSEQEgIiElDAlrNCAEhEGMExKQx7lyZJgSEQEgIiElDAlrNCAEhEGMExKQx7lyZJgSEQEgIiElDAlrNCAEhEGMExKQx7lyZJgSEQEgIiElDAlrNCAEhEGMExKQx7lyZJgSEQEgIiElDAlrNCAEhEGMExKQx7lyZJgSEQEgIiElDAlrNCAEhEGME/h8rTUhq6/Ja4gAAAABJRU5ErkJggg==" alt="" />

这里在状态矩阵中加了一个永远为 $1$ 的值,用于辅助 $i$ 每次加一。

 #include<map>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<vector>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define LL long long
#define RE register
#define IL inline
using namespace std; LL n,m;
struct mat
{
LL a[][];
mat(){for (int i=;i<;i++)for (int j=;j<;j++) a[i][j]=;}
mat operator * (const mat&b)
{
mat ans;
for (int i=;i<;i++)
for (int j=;j<;j++)
for (int k=;k<;k++)
ans.a[i][j]=(ans.a[i][j]+(a[i][k]*b.a[k][j])%m)%m;
return ans;
}
}S; int main()
{
S.a[][]=S.a[][]=;
scanf("%lld%lld",&n,&m);
LL now=;
for (LL s=;now<n;s*=)
{
mat T;
T.a[][]=T.a[][]=T.a[][]=T.a[][]=;
T.a[][]=s%m;
LL nd=min(s-,n)-now;
while (nd)
{
if (nd&) S=S*T;
T=T*T;
nd>>=;
}
now=min(s-,n);
}
printf("%lld\n",S.a[][]);
return ;
}

[HNOI 2011]数学作业的更多相关文章

  1. codevs 2314 数学作业

    2314 数学作业 2011年省队选拔赛湖南  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master     题目描述 Description 小 C 数学成绩优异 ...

  2. BZOJ-2326 数学作业 矩阵乘法快速幂+快速乘

    2326: [HNOI2011]数学作业 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 1564 Solved: 910 [Submit][Statu ...

  3. bzoj2326: [HNOI2011]数学作业

    矩阵快速幂,分1-9,10-99...看黄学长的代码理解...然而他直接把答案保存在最后一行(没有说明...好吧应该是我智障这都不知道... #include<cstdio> #inclu ...

  4. BZOJ 2326: [HNOI2011]数学作业( 矩阵快速幂 )

    BZOJ先剧透了是矩阵乘法...这道题显然可以f(x) = f(x-1)*10t+x ,其中t表示x有多少位. 这个递推式可以变成这样的矩阵...(不会用公式编辑器...), 我们把位数相同的一起处理 ...

  5. CJOJ 1331 【HNOI2011】数学作业 / Luogu 3216 【HNOI2011】数学作业 / HYSBZ 2326 数学作业(递推,矩阵)

    CJOJ 1331 [HNOI2011]数学作业 / Luogu 3216 [HNOI2011]数学作业 / HYSBZ 2326 数学作业(递推,矩阵) Description 小 C 数学成绩优异 ...

  6. [luogu P3216] [HNOI2011]数学作业

    [luogu P3216] [HNOI2011]数学作业 题目描述 小 C 数学成绩优异,于是老师给小 C 留了一道非常难的数学作业题: 给定正整数 N 和 M,要求计算 Concatenate (1 ...

  7. P3216 [HNOI2011]数学作业 (矩阵快速幂)

    P3216 [HNOI2011]数学作业 题目描述 小 C 数学成绩优异,于是老师给小 C 留了一道非常难的数学作业题: 给定正整数 NN 和 MM ,要求计算 Concatenate (1 .. N ...

  8. BZOJ 2742: [HEOI2012]Akai的数学作业

    2742: [HEOI2012]Akai的数学作业 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 535  Solved: 226[Submit][S ...

  9. [HNOI2011]数学作业 --- 矩阵优化

    [HNOI2011]数学作业 题目描述: 小 C 数学成绩优异,于是老师给小 C 留了一道非常难的数学作业题: 给定正整数 N 和 M ,要求计算\(Concatenate(1..N)\; Mod\; ...

随机推荐

  1. JavaScript(第二十天)【DOM操作表格及样式】

    DOM在操作生成HTML上,还是比较简明的.不过,由于浏览器总是存在兼容和陷阱,导致最终的操作就不是那么简单方便了.本章主要了解一下DOM操作表格和样式的一些知识. 一.操作表格 <table& ...

  2. 听翁恺老师mooc笔记(15)--文件的输入与输出

    <>重定向 如果使用标准的printf输出,有一个比较简便的方法,可以将程序的结果写入一个文件.使用<和>符号,将程序运行结果重定向到文件中去,具体使用到的代码如下: ./te ...

  3. 听翁恺老师mooc笔记(3)--指针的定义

    在上一个blog学习了&运算符,使用&取了变量.数组等地址,有什么用那?如果能够将取得的变量的地址传递给函数,能否通过这个地址在函数内访问到外部这个变量?答案是肯定的,scanf(&q ...

  4. bug终结者 团队作业第三周

    bug终结者 团队作业第三周 团队展示 队名 bug终结者 队员风采: 杨京典 20162302 风格:先构建框架,在一 一实现,在实现的过程中不断测试和修改. 擅长的技术:拆分问题,使用相对简单的思 ...

  5. 20162327WJH第五周作业

    学号 20162327 <程序设计与数据结构>第5周学习总结 教材学习内容总结 1.java是一种面向对象的语言.面向对象是一种编程方法.更是一种思维方式. 2.面向对象编程的终极目标是消 ...

  6. vim配置强悍来袭

    vim   这个关键字,我不想再过多的解释,相信看到这里的同仁,对vim都有十七八分的理解,如果你还不知道vim是什么,自己找个黑屋子... 废话不多说,今天在这里主要说vim的,不带插件的配置,也就 ...

  7. AWS EC2服务器的HTTPS负载均衡器配置过程

    AWS EC2服务器配置负载均衡器步骤:   1.普通负载均衡器   至少两台EC2实例,这里以Centos6.7系统为例 启动之后先安装个apache的httpd服务器默认80端口,或者使用其他服务 ...

  8. 服务器数据恢复_服务器xfs数据丢失数据恢复

    简介:太原一家公司的服务器出现故障,服务器是linux服务器,连接了一台某型号的存储,文件系统为xfs文件系统.管理员使用xfs_repair工具试图对文件系统进行修复但修复失败,linux服务器中所 ...

  9. linux的链接工具secure设置字体大小和颜色

  10. Python内置函数(48)——__import__

    英文文档: __import__(name, globals=None, locals=None, fromlist=(), level=0) This function is invoked by ...