CF_327C_MagicFive
来源:互联网 发布:婴儿被子品牌知乎 编辑:程序博客网 时间:2024/06/05 07:38
Magic Five
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 by5. Note that, the resulting number may contain leading zeros.
Now Iahub wants to count the number of ways he can obtain magic number, modulo1000000007 (109 + 7). Two ways are different, if the set of deleted positions ins differs.
Look at the input part of the statement, s is given in a special form.
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 plates is formed by concatenatingk copies of a together. That isn = |a|·k.
Print a single integer — the required number of ways modulo 1000000007 (109 + 7).
12561
4
139902
528
5552
63
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 are26 - 1 = 63 possible ways to delete digits.
这个题目是一道快速幂的问题
注意题中重复后是一个等比数列求和问题
因为MO是质数可以用费马小定理处理除法
#include <iostream>#include <stdio.h>#include <string.h>using namespace std;typedef long long LL;const int MO=1000000007; //注意这是一个质数const int M=100005; //这里要看明白给的是串的长度char a[M];LL qp(LL m,LL n){ LL r=1; while(n>0) { if(n&1) r=(r*m)%MO; m=(m*m)%MO; n>>=1; } return r;}LL db(LL w,LL k){ LL tmp=qp(2,w); LL re=qp(tmp,k); re=(1-re+MO)%MO; tmp=(1-tmp+MO)%MO; return re*qp(tmp,MO-2)%MO; //这里使用了费马小定理因为题中MO是个质数}int main(){ int k; int nd,w; LL tot; while(scanf("%s%d",a,&k)!=EOF) { tot=0; w=strlen(a);nd=0; for(int i=0;i<w;i++) { if(a[i]=='0'||a[i]=='5') { nd++; tot=(tot+qp(2,i))%MO; } } printf("%I64d\n",tot*db(w,k)%MO); } return 0;}
或者用分治来处理
int dfs(ll cur){ if(cur==1)return x+1; if(cur==0)return 1;int m=(cur-1)/2; int temp=dfs(m); int ret=(ll)temp*Pow(x,m+1)%Mod;; ret=(ret+temp)%Mod; if(!(cur&1LL)){ ret=ret+Pow(x,cur); ret%=Mod; } return ret;}
- CF_327C_MagicFive
- STL学习记录(十三):排序类算法及基于排序的算法
- 连载《一个程序猿的生命周期》-25.到工业现场学习业务知识引发的思考
- Single Number II
- aircrack-ng判断网卡是否兼容
- java学习基础 Collection容器的区别
- CF_327C_MagicFive
- HDU 1203 01背包
- 经典算法研究系列:九、图像特征提取与匹配之SIFT算法
- XML DTD和XML SCHEMA以及命名空间的作用
- 套接字IO模型(二) WSAAsynSelect模型
- HDU --1157 Who's in the Middle——nth_element
- 黑马程序员--学习C语言条件编译
- UIButton
- 第6讲 Map、Tuple操作