题目链接:https://atcoder.jp/contests/abc129/tasks/abc129_f

题目大意

  给定一个长度为 L ,首项为 A,公差为 B 的等差数列 S,将这 L 个数拼起来,记作 N,求 N % M。

分析

  设 bit(i) 为第 i 项所需要进行的十进制位移。
  则 $N = S_0 * 10^{bit(0)} + S_1 * 10^{bit(1)} + \dots + S_{L - 1} * 10^{bit(L - 1)}$。
  一项一项地算是肯定要超时的,不过注意到等差数列的每一项都小于 1018 ,因此很多项的长度是相等的,也就是说有很多 bit(i) 也是等差数列。
  于是我们可以按照位数给等差数列分组,最多可分 18 组。
  举个例子,在区间 [L, R] 上,每一项长度都为 k。
  记这个区间上所表示的数为 A(k),A(k) 的每一项设为 $a_i, (L \leq i \leq R)$,则 $a_i = S_i * 10^{bit(i)}, A(k) = \sum_{i = L}^{R} a_i$。
  不难看出$A(k) = 10^{bit(L)} * \sum_{i = L}^{R} (S_i * 10^{(R - i) * k})$,只要处理后一部分即可。
  设 $ret(j) = \sum_{i = L}^{j} (S_i * 10^{(j - i) * k}), (L \leq j \leq R)$。
  则有 $ret(j + 1) = ret(j) * 10^k + S_{j + 1}$,不妨设 ret(L - 1) = 0。
  于是我们可以构造如下系数矩阵 X:
$$
X = \begin{bmatrix}
10^k & 0 & 0 \\
1 & 1 & 0 \\
0 & B & 1 \\
\end{bmatrix}
$$
  和如下矩阵 RET(j):
$$
RET(j) = \begin{bmatrix}
ret(j) & S_{j + 1} & 1
\end{bmatrix} 
$$

  于是有:

$$
RET(j) = RET(j - 1) * X \\
RET(j) = RET(L - 1) * X^{j - L + 1}
$$

  如此,通过矩阵快速幂,长度为 k 的一组值很快就被算出来了,然后每一组都分别算一下再加起来即可。

  PS:在实际实现过程中组与组之间是可以合并的,并不需要单独算出来每一组的余数,详细实现请看代码。

代码如下

 #include <bits/stdc++.h>
using namespace std; #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define Rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
#define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define REMOVE(x, c) x.erase(remove(x.begin(), x.end(), c), x.end()); // 删去 x 中所有 c
#define TOLOWER(x) transform(x.begin(), x.end(), x.begin(),::tolower);
#define TOUPPER(x) transform(x.begin(), x.end(), x.begin(),::toupper); #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) #define MP make_pair
#define PB push_back
#define ft first
#define sd second template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
} template<typename T>
istream &operator>>(istream &in, vector<T> &v) {
for (auto &x: v)
in >> x;
return in;
} template<typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
out << "[" << p.first << ", " << p.second << "]" << "\n";
return out;
} inline int gc(){
static const int BUF = 1e7;
static char buf[BUF], *bg = buf + BUF, *ed = bg; if(bg == ed) fread(bg = buf, , BUF, stdin);
return *bg++;
} inline int ri(){
int x = , f = , c = gc();
for(; c<||c>; f = c=='-'?-:f, c=gc());
for(; c>&&c<; x = x* + c - , c=gc());
return x*f;
} template<class T>
inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
} inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
} //min <= aim <= max
template<typename T>
inline bool BETWEEN(const T aim, const T min, const T max) {
return min <= aim && aim <= max;
} typedef long long LL;
typedef unsigned long long uLL;
typedef pair< double, double > PDD;
typedef pair< int, int > PII;
typedef pair< int, PII > PIPII;
typedef pair< string, int > PSI;
typedef pair< int, PSI > PIPSI;
typedef set< int > SI;
typedef set< PII > SPII;
typedef vector< int > VI;
typedef vector< double > VD;
typedef vector< VI > VVI;
typedef vector< SI > VSI;
typedef vector< PII > VPII;
typedef map< int, int > MII;
typedef map< int, string > MIS;
typedef map< int, PII > MIPII;
typedef map< PII, int > MPIII;
typedef map< string, int > MSI;
typedef map< string, string > MSS;
typedef map< PII, string > MPIIS;
typedef map< PII, PII > MPIIPII;
typedef multimap< int, int > MMII;
typedef multimap< string, int > MMSI;
//typedef unordered_map< int, int > uMII;
typedef pair< LL, LL > PLL;
typedef vector< LL > VL;
typedef vector< VL > VVL;
typedef priority_queue< int > PQIMax;
typedef priority_queue< int, VI, greater< int > > PQIMin;
const double EPS = 1e-;
const LL inf = 0x7fffffff;
const LL infLL = 0x7fffffffffffffffLL;
LL mod = 1e9 + ;
const int maxN = 1e5 + ;
const LL ONE = ;
const LL evenBits = 0xaaaaaaaaaaaaaaaa;
const LL oddBits = 0x5555555555555555; struct Matrix{
int row, col;
LL MOD;
VVL mat; Matrix(int r, int c, LL p = mod) : row(r), col(c), MOD(p) {
mat.assign(r, VL(c, ));
}
Matrix(const Matrix &x, LL p = mod) : MOD(p){
mat = x.mat;
row = x.row;
col = x.col;
}
Matrix(const VVL &A, LL p = mod) : MOD(p){
mat = A;
row = A.size();
col = A[].size();
} // x * 单位阵
inline void E(int x = ) {
assert(row == col);
Rep(i, row) mat[i][i] = x;
} inline VL& operator[] (int x) {
assert(x >= && x < row);
return mat[x];
} inline Matrix operator= (const VVL &x) {
row = x.size();
col = x[].size();
mat = x;
return *this;
} inline Matrix operator+ (const Matrix &x) {
assert(row == x.row && col == x.col);
Matrix ret(row, col);
Rep(i, row) {
Rep(j, col) {
ret.mat[i][j] = mat[i][j] + x.mat[i][j];
ret.mat[i][j] %= MOD;
}
}
return ret;
} inline Matrix operator* (const Matrix &x) {
assert(col == x.row);
Matrix ret(row, x.col);
Rep(k, x.col) {
Rep(i, row) {
if(mat[i][k] == ) continue;
Rep(j, x.col) {
ret.mat[i][j] += mat[i][k] * x.mat[k][j];
ret.mat[i][j] %= MOD;
}
}
}
return ret;
} inline Matrix operator*= (const Matrix &x) { return *this = *this * x; }
inline Matrix operator+= (const Matrix &x) { return *this = *this + x; } inline void print() {
Rep(i, row) {
Rep(j, col) {
cout << mat[i][j] << " ";
}
cout << endl;
}
}
}; // 矩阵快速幂,计算x^y
inline Matrix mat_pow_mod(Matrix x, LL y) {
Matrix ret(x.row, x.col);
ret.E();
while(y){
if(y & ) ret *= x;
x *= x;
y >>= ;
}
return ret;
} LL L, A, B, M, ans; // 从数列第 st 项开始,查找区间 [L, R],使得区间内的所有数都小于 bit 大于等于 bit/10。
// 有返回 true 没有返回 false
LL st = , bit = , l, r;
bool getLR() {
if(st >= L || A + st * B >= bit) return false;
l = st;
r = L - ; while(l < r) {
LL mid = (l + r) >> ;
if(A + mid * B < bit) l = mid + ;
else r = mid;
} if(A + r * B >= bit) --r;
l = st;
st = r + ; // 下一个起始位置
return true;
} int main(){
//freopen("MyOutput.txt","w",stdout);
//freopen("input.txt","r",stdin);
INIT();
cin >> L >> A >> B >> mod; For(i, , ) { // 枚举位数
if(getLR()) {
Matrix mat(, );
mat[][] = bit % mod;
mat[][] = mat[][] = mat[][] = mat[][] = ;
mat[][] = mat[][] = mat[][] = ;
mat[][] = B % mod; mat = mat_pow_mod(mat, r - l + ); Matrix ret(, );
ret[][] = ans;
ret[][] = (A + l * B) % mod;
ret[][] = ; ret *= mat; ans = ret[][];
}
bit *= ;
}
cout << ans << endl;
return ;
}

AtCoder ABC 129F Takahashi's Basics in Education and Learning的更多相关文章

  1. AtCoder ABC 250 总结

    AtCoder ABC 250 总结 总体 连续若干次一样的结果:30min 切前 4 题,剩下卡在 T5 这几次卡在 T5 都是一次比一次接近, 什么 dp 前缀和打挂,精度被卡,能水过的题连水法都 ...

  2. ATCODER ABC 099

    ATCODER ABC 099 记录一下自己第一场AK的比赛吧...虽然还是被各种踩... 只能说ABC确实是比较容易. A 题目大意 给你一个数(1~1999),让你判断它是不是大于999. Sol ...

  3. Atcoder ABC 141

    Atcoder ABC 141 A - Weather Prediction SB题啊,不讲. #include<iostream> #include<cstdio> #inc ...

  4. Atcoder ABC 139E

    Atcoder ABC 139E 题意: n支球队大循环赛,每支队伍一天只能打一场,求最少几天能打完. 解法: 考虑抽象图论模型,既然一天只能打一场,那么就把每一支球队和它需要交手的球队连边. 求出拓 ...

  5. Atcoder ABC 139D

    Atcoder ABC 139D 解法: 等差数列求和公式,记得开 $ long long $ CODE: #include<iostream> #include<cstdio> ...

  6. Atcoder ABC 139C

    Atcoder ABC 139C 题意: 有 $ n $ 个正方形,选择一个起始位置,使得从这个位置向右的小于等于这个正方形的高度的数量最多. 解法: 简单递推. CODE: #include< ...

  7. Atcoder ABC 139B

    Atcoder ABC 139B 题意: 一开始有1个插口,你的插排有 $ a $ 个插口,你需要 $ b $ 个插口,问你最少需要多少个插排. 解法: 暴力模拟. CODE: #include< ...

  8. Atcoder ABC 139A

    Atcoder ABC 139A 题意: 给你两个字符串,记录对应位置字符相同的个数 $ (n=3) $ 解法: 暴力枚举. CODE: #include<iostream> #inclu ...

  9. atcoder abc 244

    atcoder abc 244 D - swap hats 给定两个 R,G,B 的排列 进行刚好 \(10^{18}\) 次操作,每一次选择两个交换 问最后能否相同 刚好 \(10^{18}\) 次 ...

随机推荐

  1. Vue.js 样式绑定(1)

    demo <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <titl ...

  2. 使用multiprocessing模块创建进程

    #_author:来童星#date:2019/12/17from multiprocessing import Processimport timeimport os#两个子进程将会调用的两个方法de ...

  3. Zookeeper_ZAB协议

    ZAB协议 ZAB协议简介 ZAB:(Zookeeper Atomic Broadcast),zk原子消息广播协议,是专为ZK设计的一中支持崩溃恢复的原子广播协议,是一种Paxos协议的优化算法,在Z ...

  4. idea使用问题

    1. 问题: 突发断电导致idea的play项目错误,无法识别build.sbt,build.sbt文件报错,Cannot resolve symbol 解决方案: For anyone having ...

  5. nginx中reuqest_uri与uri的区别说明

    reuqest_uri:即客户端发送来的原生请求URI,包括请求参数 uri:请求URI,不包括任何请求参数 举例说明: 1.比如客户端以 get 方式请求 /admin 页面,并且带 id 和 na ...

  6. Spring源码由浅入深系列二 类结构

    BeanFactory 上一章中,我们提过Spring的依赖注入容器是BeanFactory.BeanFactory是一个基础接口,它有一个默认实现类:DefaultListableBeanFacto ...

  7. C/S通信

    一直在考虑写一个服务端和客户端通信的框架,就现在的需求,打算走http协议. 通信方式打算用Key/Value的形式. 这里面其实还是有很多的问题的,这样的一个通信框架其实是SOA的一部分.其他 但是 ...

  8. python 15 文件操作(一)

    转自 http://www.cnblogs.com/BeginMan/p/3166644.html 一.文件对象 我理解的文件对象就是一个接口,通过这个接口对文件进行相关操作. <Python ...

  9. neo4j数据库迁移---------Neo4j数据库导入导出的方法

    Neo4j数据进行备份.还原.迁移的操作时,首先要关闭neo4j; /usr/share/neo4j/bin neo4j stop 如果出现 Neo4j not running 出现这种情况, Neo ...

  10. IRP FLAGS

    IRP所有标识位的含义,是 _IRP . flags 这个成员 IRP_NOCACHE 0x00000001 //表示I/O请求从存储的媒介而不是高速缓存中读取数据 IRP_PAGING_IO 0x0 ...