hnust 1713 Additions(矩阵快速幂)

传送门: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]
并且由于结果太大取模,可用此模板

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值