Magic Five CodeForces
来源:互联网 发布:网络黄金egd裴蕾抓了吗 编辑:程序博客网 时间:2024/05/16 06:44
题目链接:点我
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).
Example
Input
12561Output4Input139902Output528Input5552Output63
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.
题意:
给你一段重复k次的数字, 问你它有多少种方式使得从中删除一些数字和能使得这个新的数字能整除5.
思路:
仔细分析我们发现如果那串数字的第 i 为是 5 或者 0,那么它前面的i - 1 个数字有删或者不删除两种情况 即:
代码:
#include<cstdio>#include<algorithm>#include<cstring>#include<iostream>#include<cmath>using namespace std;char ss[100000+10];typedef long long LL;const int mod = 1e9 + 7;LL qpow(LL x,LL n){ LL ans = 1; while(n){ if(n&1) ans = ans * x % mod; x = x* x% mod; n >>= 1; } return ans;}int main(){ LL k; scanf("%s %I64d", ss,&k); int l = 0; int len =strlen(ss); LL cnt = 0; for(int i = 0; i<len;++i) if(ss[i]=='0'||ss[i] == '5') cnt =(cnt + qpow(2,i)) % mod; //首项 LL x = qpow(2,len); //公比 LL y = qpow(x,k); x=(1-x + mod) % mod; y= (1-y + mod ) % mod; LL ans = cnt * (y * qpow(x, mod-2) %mod) %mod; printf("%I64d\n",(ans + mod) % mod); return 0;}
阅读全文
0 0
- codeforces Magic Five
- Magic Five CodeForces
- codeforces 327C Magic Five
- codeforces 327C. Magic Five
- 【数论】codeforces 327C Magic five
- CodeForces 327C Magic Five 题解&代码
- codeforces 327C Magic Five 分数形式整数取余
- Codeforces Round #191 (Div. 2) C magic five
- codeforces#191_div2_C Magic Five 矩阵快速幂
- CodeForces 327C Magic Five 二分求和 或 矩阵求和
- Codeforces Round #191 (Div. 2) C. Magic Five
- codeforces #327C Magic Five 矩阵快速幂加费马小定理加逆元
- Codeforces Round #191 (Div. 2) C. Magic Five
- Codeforces Round #191 (Div. 2) C. Magic Five
- CodeForces 327 C.Magic Five(快速幂)
- C. Magic Five
- Codeforces Round #191 (Div. 2) C. Magic Five 等比数列的快速幂
- CodeForces Round #191 (327C) - Magic Five 等比数列求和的快速幂取模
- 139. Word Break
- java8 函数式编程
- 自动装箱与自动拆箱
- Ajax学习
- JVM垃圾回收算法
- Magic Five CodeForces
- 例题1.12 组装电脑 Assemble UVALive
- 【Java线程】锁机制:synchronized、Lock、Condition
- 学习笔记—诊断机器学习模型
- Java电话本管理系统(数组版)
- java多线程Runnable入门实例
- json 解析报错
- 将三个数有序输出
- java面试题总结