AtCoder ABC 129F Takahashi's Basics in Education and Learning
题目链接:https://atcoder.jp/contests/abc129/tasks/abc129_f
题目大意
给定一个长度为 L ,首项为 A,公差为 B 的等差数列 S,将这 L 个数拼起来,记作 N,求 N % M。
分析
X = \begin{bmatrix}
10^k & 0 & 0 \\
1 & 1 & 0 \\
0 & B & 1 \\
\end{bmatrix}
$$
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的更多相关文章
- AtCoder ABC 250 总结
AtCoder ABC 250 总结 总体 连续若干次一样的结果:30min 切前 4 题,剩下卡在 T5 这几次卡在 T5 都是一次比一次接近, 什么 dp 前缀和打挂,精度被卡,能水过的题连水法都 ...
- ATCODER ABC 099
ATCODER ABC 099 记录一下自己第一场AK的比赛吧...虽然还是被各种踩... 只能说ABC确实是比较容易. A 题目大意 给你一个数(1~1999),让你判断它是不是大于999. Sol ...
- Atcoder ABC 141
Atcoder ABC 141 A - Weather Prediction SB题啊,不讲. #include<iostream> #include<cstdio> #inc ...
- Atcoder ABC 139E
Atcoder ABC 139E 题意: n支球队大循环赛,每支队伍一天只能打一场,求最少几天能打完. 解法: 考虑抽象图论模型,既然一天只能打一场,那么就把每一支球队和它需要交手的球队连边. 求出拓 ...
- Atcoder ABC 139D
Atcoder ABC 139D 解法: 等差数列求和公式,记得开 $ long long $ CODE: #include<iostream> #include<cstdio> ...
- Atcoder ABC 139C
Atcoder ABC 139C 题意: 有 $ n $ 个正方形,选择一个起始位置,使得从这个位置向右的小于等于这个正方形的高度的数量最多. 解法: 简单递推. CODE: #include< ...
- Atcoder ABC 139B
Atcoder ABC 139B 题意: 一开始有1个插口,你的插排有 $ a $ 个插口,你需要 $ b $ 个插口,问你最少需要多少个插排. 解法: 暴力模拟. CODE: #include< ...
- Atcoder ABC 139A
Atcoder ABC 139A 题意: 给你两个字符串,记录对应位置字符相同的个数 $ (n=3) $ 解法: 暴力枚举. CODE: #include<iostream> #inclu ...
- atcoder abc 244
atcoder abc 244 D - swap hats 给定两个 R,G,B 的排列 进行刚好 \(10^{18}\) 次操作,每一次选择两个交换 问最后能否相同 刚好 \(10^{18}\) 次 ...
随机推荐
- flink on yarn启动失败
我启动hadoop on yarn 集群后 [root@node1 flink-1.6.1]# ./bin/yarn-session.sh -n 2 -jm 1024 -tm 1024 报的如下错误 ...
- [转]C# 动态调用 WebService
通常我们在程序中需要调用WebService时,都是通过“添加Web引用”,让VS.NET环境来为我们生成服务代理,然后调用对应的Web服务.这样是使工作简单了,但是却和提供Web服务的URL.方法名 ...
- pymupdf 修改pdf文件
安装: sudo pip install pymupdf==1.16.0 引入使用: import fitz 可以插入文字.图片.... 帮助文档: PyMuPDF documentation ht ...
- python模块:typing
很多人在写完代码一段时间后回过头看代码,很可能忘记了自己写的函数需要传什么参数,返回什么类型的结果,就不得不去阅读代码的具体内容,降低了阅读的速度,加上Python本身就是一门弱类型的语言,这种现象就 ...
- 树形dp换根,求切断任意边形成的两个子树的直径——hdu6686
换根dp就是先任取一点为根,预处理出一些信息,然后在第二次dfs过程中进行状态的转移处理 本题难点在于任意割断一条边,求出剩下两棵子树的直径: 设割断的边为(u,v),设down[v]为以v为根的子树 ...
- storm集群的安装
storm图解 storm的基本概念 Topologies:拓扑,也俗称一个任务 Spoults:拓扑的消息源 Bolts:拓扑的处理逻辑单元 tuple:消息元组,在Spoults和Bolts传递数 ...
- flume注意的事项
bin/flume-ng agent --conf conf/ --name a3 --conf-file conf/flume-dir.conf & 在使用Spooling Director ...
- NX二次开发-NX+VS写代码设断点调试技巧
在做NX二次开发的时候写完代码,编译可以通过,但是执行的时候却没有反应,或者得到的结果不对,说明肯定有地方传值出错了.我在查找代码错误的时候有几种方法:1.uc1601打印函数输入和输出的值看对不对. ...
- Python3中string内置参数
说明: 使用ipython查看python3的内置函数 ,只需要输入字符串按两下tab键 capitalize():将字符串中第一个字符大写 casefold:将字符串中的所有大写字母转为小写 cen ...
- Web UI 设计(网页设计)命名规范
Web UI 设计命名规范 一.网站设计及基本框架结构: 1. Container“container“ 就是将页面中的所有元素包在一起的部分,这部分还可以命名为: “wrapper“, “wr ...