Codeforces Round #191 (Div. 2) C. Magic Five
2016-05-10 17:18
309 查看
There is a long plate s containing
n digits. Iahub wants to delete some digits (possibly none, but he is not allowed to delete all the digits) to form his "magic number" on the plate, a number that is divisible by
5. Note that, the resulting number may contain leading zeros.
Now Iahub wants to count the number of ways he can obtain magic number, modulo
1000000007 (109 + 7). Two ways are different, if the set of deleted positions in
s differs.
Look at the input part of the statement, s is given in a special form.
Input
In the first line you're given a string a (1 ≤ |a| ≤ 105), containing digits only. In the second line you're given an integer
k (1 ≤ k ≤ 109). The plate
s is formed by concatenating
k copies of a together. That is
n = |a|·k.
Output
Print a single integer — the required number of ways modulo
1000000007 (109 + 7).
Examples
Input
Output
Input
Output
Input
Output
Note
In the first case, there are four possible ways to make a number that is divisible by 5: 5, 15, 25 and 125.
In the second case, remember to concatenate the copies of
a. The actual plate is 1399013990.
In the third case, except deleting all digits, any choice will do. Therefore there are
26 - 1 = 63 possible ways to delete digits.
分析:快速幂费马小定理乱搞一番。
n digits. Iahub wants to delete some digits (possibly none, but he is not allowed to delete all the digits) to form his "magic number" on the plate, a number that is divisible by
5. Note that, the resulting number may contain leading zeros.
Now Iahub wants to count the number of ways he can obtain magic number, modulo
1000000007 (109 + 7). Two ways are different, if the set of deleted positions in
s differs.
Look at the input part of the statement, s is given in a special form.
Input
In the first line you're given a string a (1 ≤ |a| ≤ 105), containing digits only. In the second line you're given an integer
k (1 ≤ k ≤ 109). The plate
s is formed by concatenating
k copies of a together. That is
n = |a|·k.
Output
Print a single integer — the required number of ways modulo
1000000007 (109 + 7).
Examples
Input
1256 1
Output
4
Input
13990 2
Output
528
Input
555 2
Output
63
Note
In the first case, there are four possible ways to make a number that is divisible by 5: 5, 15, 25 and 125.
In the second case, remember to concatenate the copies of
a. The actual plate is 1399013990.
In the third case, except deleting all digits, any choice will do. Therefore there are
26 - 1 = 63 possible ways to delete digits.
分析:快速幂费马小定理乱搞一番。
#include <cstdio> #include <iostream> #include <cstring> #define MAXN 1000000007 using namespace std; char s[100005]; long long k,ans; long long ksm(long long a,long long b) { long long ans = 1; while(b) { if(b & 1 == 1) ans = (ans * a) % MAXN; a = a*a % MAXN; b >>= 1; } return ans; } int main() { cin.sync_with_stdio(false); cin>>s; long long n = strlen(s); cin>>k; long long plus = ((ksm(2,n*k) - 1)*ksm((ksm(2,n)-1+MAXN)%MAXN,MAXN-2) % MAXN + MAXN) % MAXN; for(int i = 0;i < n;i++) if(s[i] == '0' || s[i] == '5') ans = (ans + ksm(2,i)*plus) % MAXN; cout<<ans<<endl; }
相关文章推荐
- javascript:history.go()和History.back()的区别及应用
- 最强ListView优化方案
- nyoj 973 天下第一
- 安装pod程序
- HP G9 系列安装系统 做阵列
- 指向学生类的指针
- 六个技巧,培养孩子乐观心态
- MFC 完全删除一个类
- java基础
- notepad++快捷键大全
- Android空包签名
- lintcode----二叉树的所有路径
- 292. Nim Game [easy] (Python)
- 关于MyEclipse中lib导包和buildpath的区别
- load&initialize&单例模式
- java1.8中Lambda表达式reduce聚合测试例子
- ThinkPHP怎样配置URL模式为PathInfo?
- mysql默认可以跨库,mysqli不可以
- 實際案例: 獲取臨時票証 (JsApi Ticket)
- Android持久化保存cookie