Magic Five

Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Submit Status

Description

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 = |ak.

Output

Print a single integer — the required number of ways modulo 1000000007 (109 + 7).

Sample Input

Input
  1. 1256
    1
Output
  1. 4
Input
  1. 13990
    2
Output
  1. 528
Input
  1. 555
    2
Output
  1. 63

Hint

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,将这K个串连接起来,然后可以删除其中一些数字,但是不能全部删除,使得这个串表示的数能被5整除,可以存在包含前导零的情况,05 和 5是两个不同的数。问总共能有多少这种数。

思路:

能被5整除,那么要么是0 要么是5结尾,所以对于只有一个串的时候每次都找0 5结尾的数,它前面的可以选或者不选就是总共2^i种可能。当有多个串时,第2,3,4,。。。k个串中可能性就是第一个串中对应位置的 i+strlen(str), 第一个串中符合条件的2^i的和为tmp,那么k个串中符合条件的总和就是  tmp*(1+2^len+2^(2len)+ 2^(3len)....+2^(klen)),这是个等比数列求和问题,可以化成(1-2^(len*k))/ (1-2^(len)) %mod

假设 a=(1-2^(len*k))b=(1-2^(len))  由于a很大,所以这个时候就要用到逆元来求(a/b)%mod

  1. //2016.8.14
  2. #include<iostream>
  3. #include<cstdio>
  4. #define ll long long
  5.  
  6. using namespace std;
  7.  
  8. const int mod = 1e9+;
  9.  
  10. ll pow(ll a, ll b)//快速幂
  11. {
  12. ll ans = ;
  13. while(b)
  14. {
  15. if(b&)ans *= a, ans %= mod;
  16. a *= a, a %= mod;
  17. b>>=;
  18. }
  19. return ans;
  20. }
  21.  
  22. int main()
  23. {
  24. string a;
  25. int k;
  26. ll ans = ;//ans = 2^i * ((i^kn)/(1-2^n))%mod
  27. while(cin>>a>>k)
  28. {
  29. ans = ;
  30. int n = a.size();
  31. for(int i = ; i < n; i++)
  32. if(a[i]==''||a[i]=='')
  33. ans+=pow(, i);
  34. ll y = pow(, n);
  35. ll x = pow(y, k);
  36. x = ((-x)%mod+mod)%mod;
  37. y = ((-y)%mod+mod)%mod;
  38. ans = ((ans%mod)*(x*pow(y, mod-)%mod))%mod;//利用费马小定理求y的逆元
  39. cout<<ans<<endl;
  40. }
  41.  
  42. return ;
  43. }

CodeForces 327C的更多相关文章

  1. (水题)Codeforces - 327C - Magic Five

    https://codeforces.com/problemset/problem/327/C 因为答案可以有前导零,所以0和5一视同仁.每个小节内,以排在第 $i$ 个的5为结尾的序列即为在前面 $ ...

  2. CodeForces Round #191 (327C) - Magic Five 等比数列求和的快速幂取模

    很久以前做过此类问题..就因为太久了..这题想了很久想不出..卡在推出等比的求和公式,有除法运算,无法快速幂取模... 看到了 http://blog.csdn.net/yangshuolll/art ...

  3. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  4. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  5. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  6. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  7. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  8. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  9. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

随机推荐

  1. (简单) POJ 1321 棋盘问题,回溯。

    Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子 ...

  2. [Unity Shader]Shader分类

    Shader的分类: Shader按管线分类一般分为固定渲染管线与可编程渲染管线 (1)固定渲染管线 ——这是标准的几何&光照(Transforming&Lighting)管线,功能是 ...

  3. Memcache第一篇---基础教程

    Memcache是什么 Memcache是danga.com的一个项目,最早是为 LiveJournal 服务的,目前全世界不少人使用这个缓存项目来构建自己大负载的网站,来分担数据库的压力. 它可以应 ...

  4. Struts2实现异步调用机制详细剖析(XML和JSON)

    一.使用XML传递 1.页面展示getXML.jsp <%@ page language="java" import="java.util.*" page ...

  5. mvn

    http://blog.csdn.net/z69183787/article/category/2265961

  6. 转载:ECMAScript 6简介

    ECMAScript 6(以下简称ES6)是JavaScript语言的下一代标准,已经在2015年6月正式发布了.它的目标,是使得JavaScript语言可以用来编写复杂的大型应用程序,成为企业级开发 ...

  7. c++读取REG_MULTI_SZ类型注册表

    First: run RegQueryValueEx to get type and necessary memory size: Single byte code: DWORD type, size ...

  8. tp 框架的增

    //增 public function test() { //数据访问 //造模型对象 $nation = D("Nation"); $,'页面跳转中请稍后....'); } pu ...

  9. stm32驱动DS1302芯片

    天时可以自动调整,且具有闰年补偿功能.工作电压宽达2.5-5.5V.采用双电源供电(主电源和备用电源),可设置备用电源充电方式,提供了对后背电源进行涓细电流充电的能力.DS1302的外部引脚分配如下图 ...

  10. JNI中的内存管理(转)

    源:JNI中的内存管理 JNI 编程简介 JNI,Java Native Interface,是 native code 的编程接口.JNI 使 Java 代码程序可以与 native code 交互 ...