C. Magic Five
来源:互联网 发布:怎么装修企业淘宝店铺 编辑:程序博客网 时间:2024/06/05 07:00
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.
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.
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 are 26 - 1 = 63 possible ways to delete digits.
code:
#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <algorithm>#include <iostream>using namespace std;int vis[100020]; char f[100020];__int64 powe_m(__int64 a,__int64 b,__int64 c) //注意要int64接收变量 否则会溢出{__int64 ans=1; __int64 tmp=a;while(b!=0){ if (b&1) ans=ans*tmp%c; //???????? ans=ans*ans%c ??????tmp=tmp*tmp%c;b=b>>1;}return ans; }int main(){int i,k; 注意要int64 变量 否则k在计算k*strlen(f)的时候会溢出 ,或者给k*strlen(f)来个强制转换最好直接在k定义的时候__int64,也是提醒我们该变量不超过int范围不一定就绝对得定义为int,还要考虑计算的过程是否超过int。__int64 ans=0; scanf("%s",f);scanf("%d",&k); for (i=strlen(f)-1;i>=0;i--){if (f[i]=='0'||f[i]=='5') vis[i]++;}int qiandao0=0;i=0;while(f[i]==0){qiandao0++;i++;} __int64 ccd= powe_m(2,strlen(f),1000000007) -1;__int64 ab= powe_m(2,(__int64)(k)*strlen(f),1000000007)-1;__int64 big= (ab*(powe_m(ccd,(1000000007-2),1000000007 ) ) )%1000000007; //神一样的公式
////除法取余方法的一种,(a/b)%mod,等价于(a*b^(mod-2))%mod,mod为素数 //如果不这样的话。a%mod / b%mod 已经和 (a/b)%mod 不相同了。
//这里有点乱。。其实按原来的话是 下面的for循环每次都要计算k次(k最大10^9) 必然超时,刚好发现能化简,最后是等比数列,所以就有了上面的乱七八糟的计算。for (i=strlen(f)-1;i>=0;i--){if (vis[i]){ ans+= powe_m (2,i,1000000007) ;ans%=1000000007; //自己原来是每次*big 其实可以再最后才*big}} printf("%I64d\n",ans*big%1000000007);return 0;}
- C. Magic Five
- 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 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(快速幂)
- codeforces Magic Five
- Magic Five CodeForces
- Codeforces327C Magic Five[组合数学]
- Codeforces Round #191 (Div. 2) C. Magic Five 等比数列的快速幂
- CodeForces Round #191 (327C) - Magic Five 等比数列求和的快速幂取模
- Python读取文件
- spark:学习杂记--23
- 对象的消息模型
- 最近距离
- 第六周程序阅读(3)
- C. Magic Five
- UML的9种图例解析
- Linux C 异常处理:信号机制 SIGFPE/signal()/raise()
- 股票风险
- nRF905自学笔记
- C++析构函数为什么要为虚函数
- 关于JSON理解和用法
- Fragment详解之五——Fragment间参数传递
- 汇编学习之路之DS