传送门:http://acm.hnust.edu.cn/JudgeOnline/problem.php?cid=1436&pid=5
时间限制: 1 Sec 内存限制: 128 MB
题目描述
Ada is 5 years old, and she is learning additions. Her father writes some exercises for her.
1+11=?
2+22=?
3+33=?
“It is easy”, she writes the answer correctly. “Try to answer the following questions”,
1+11+111=?
2+22+222+2222=?
Ada scratches her head. “It’s too difficult”.
So, help Ada to compute the value of equation a+aa+aaa+… which have n items. Since the answer may be quite large, you have to module it by an integer m.
输入
Input contains at most 1000 test cases. Each test case contains only one line.
Each line will contain three integers a, n and m. (1<=a<=9, 1<=n<231-1, 1<=m<=100007). Process to end of file.
输出
For each test cases, output the value of (a+aa+aaa+… .)%m。
样例输入
1 1 13
1 2 13
1 3 13
2 2 13
2 3 13
样例输出
1
12
6
11
12
分析:
1.F(n)=(a+aa+aaa+……+aaa……a)%m
通过化简可以得出一个式子
结果为a*([10^(n+1)-10-9*n]/81)%m
结合n的范围,需要用快速幂和逆元。。 求逆元的时候需要m和81互质,m范围是[1,100007],所以不一定互质,此方法可能WA……
2.重新写一下
F(n)=(a+aa+aaa+……+aaa……a)%m;
F(n-1)=(a+aa+aaa+……+aa……a)%m;
令A[1]=a
A[n]=10*A[n-1]
F(n)=f(n-1)+10*A[n-1]+a
以A[n],F[n],a为矩阵元素 可用矩阵快速幂模板
模板:http://blog.csdn.net/feng_zhiyu/article/details/76400635
小结:
根据题中的式子化简
涉及 结果F(n),项A[n] 类似斐波那契数列的 F(n) 和 a[n]
并且由于结果太大取模,可用此模板