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
1256
1
Output
4
Input
13990
2
Output
528
Input
555
2
Output
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

 //2016.8.14
#include<iostream>
#include<cstdio>
#define ll long long using namespace std; const int mod = 1e9+; ll pow(ll a, ll b)//快速幂
{
ll ans = ;
while(b)
{
if(b&)ans *= a, ans %= mod;
a *= a, a %= mod;
b>>=;
}
return ans;
} int main()
{
string a;
int k;
ll ans = ;//ans = 2^i * ((i^kn)/(1-2^n))%mod
while(cin>>a>>k)
{
ans = ;
int n = a.size();
for(int i = ; i < n; i++)
if(a[i]==''||a[i]=='')
ans+=pow(, i);
ll y = pow(, n);
ll x = pow(y, k);
x = ((-x)%mod+mod)%mod;
y = ((-y)%mod+mod)%mod;
ans = ((ans%mod)*(x*pow(y, mod-)%mod))%mod;//利用费马小定理求y的逆元
cout<<ans<<endl;
} return ;
}

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. CodeForces 620E New Year Tree

    线段树+位运算 首先对树进行DFS,写出DFS序列,记录下每一个节点控制的区间范围.然后就是区间更新和区间查询了. 某段区间的颜色种类可以用位运算来表示,方便计算. 如果仅有第i种颜色,那么就用十进制 ...

  2. Pop框架简述

    Facebook发布了Paper之后,进一步开源了其背后的动画引擎Pop,此框架并不满足于苹果自身的动画单调性,致力于给用户一种逼真的动画效果,可以减少用户对于苹果原生Core Animation 复 ...

  3. iOS开发——NSArray中的字典排序

    手头上碰到一个项目,需要给数组中的字典中的一个字段排序,想了想,干脆再字典中增加一个字段,用来记录需要排序字段的第一个字符,用它来作为比较的对象,进行排序. - (void)viewDidLoad { ...

  4. OC语言的特性(一)-消息传递与调用函数的表现形式

    我们在初学Objective-C时,都会觉得ObjC中的消息传递和其他语言的调用函数差不多,只是在OC中,方法调用用消息传递这一概念来代替. 那么到底怎样区别OC中的消息传递与其他语言的调用函数呢. ...

  5. php上传zip文件在线解压文件在指定目录下,CI框架版本

    我从网上找的文件php在线解压zip压缩文件 文件为jy.php可以直接执行,但是怎样将其加到CI框架中呢?? jy.php文件 <?php header("content-Type: ...

  6. laravel无法显示路由界面

    安装完laravel项目后,开启了重写,/app/storage也设置好了权限,但是始终无法显示出页面,并出现: "Whoops, looks like something went wro ...

  7. 使用LIBUSB实现和自定义通讯设备通讯--MFC代码在末尾

    LIBUSB是一款简单好用的USB通讯开发库,一般HID设备用该库通讯能大大降低开发周期,使用如下,首先需要为设备安装驱动 在libusb的bin目录下有一个inf_wirzed.exe的文件,该文件 ...

  8. css3动画-animation

    animation驱使一组css style变化到另外一组css style,它可以定义keyframes的集合,指定style的开始和结束状态,它是transition的增强. 配置animatio ...

  9. Android线程之Thread 、Runnable 的两个例子

    Thread例子: package fy.test; import android.app.Activity; import android.os.Bundle; import android.os. ...

  10. Spring Junit 读取WEB-INF下的配置文件

    假设Spring配置文件为applicationContext.xml 一.Spring配置文件在类路径下面 在Spring的java应用程序中,一般我们的Spring的配置文件都是放在放在类路径下面 ...