C. Magic Five

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 524288/262144K (Java/Other)
Total Submission(s) : 12   Accepted Submission(s) : 3
Problem 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=|a|k.

 
Output

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

 
Sample Input
1256
1
13990
2
 
Sample Output
4
528
 
/**

Codeforces Round #191 (Div. 2) C magic five
 
题目大意:给定一个字符串 s,长度 len 最长是100000,接着输入一个整数 k,k 最大是10的9次方,
求 k 个 s 连接在一起得到一个串,设为str,从str中删除一些数(也可以不删,但不能全部删除),
使得得到的数能够整除5,求共有多少种删除方式,注意:被删除的数的位置不同,算做不同的删除方式。
 
解题思路:我们知道如果一个数能够整除5,那它的个位数一定是 0 或者 5 。
因此我们需要在串 s 中找到每个 0 或者 5 的位置,假设第一个找到的位置为 i ,
易证明,以此位置的0(或者5)为尾数共有 2 的 i 次方种删除方式,
进而可得由 k 个 s 组成的串 str 中有 2 的 i 次幂 + 2 的 (i+len*1)次幂 + …… + 2 的 [ i + len *(k-1)] 次幂,
易发现这是一个首项为2的 i 次幂,公比为 2 的 len 次幂的等比数列求和,我们用快速幂和乘法逆元解决。
所以对串 s 中每个0 或者5 进行计算并将结果相加,即可得到最后答案。
 
 
/**
Codeforces Round #191 (Div. 2) C magic five 题目大意:给定一个字符串 s,长度 len 最长是100000,接着输入一个整数 k,k 最大是10的9次方,
求 k 个 s 连接在一起得到一个串,设为str,从str中删除一些数(也可以不删,但不能全部删除),
使得得到的数能够整除5,求共有多少种删除方式,注意:被删除的数的位置不同,算做不同的删除方式。 解题思路:我们知道如果一个数能够整除5,那它的个位数一定是 0 或者 5 。
因此我们需要在串 s 中找到每个 0 或者 5 的位置,假设第一个找到的位置为 i ,
易证明,以此位置的0(或者5)为尾数共有 2 的 i 次方种删除方式,
进而可得由 k 个 s 组成的串 str 中有 2 的 i 次幂 + 2 的 (i+len*1)次幂 + …… + 2 的 [ i + len *(k-1)] 次幂,
易发现这是一个首项为2的 i 次幂,公比为 2 的 len 次幂的等比数列求和,我们用快速幂和乘法逆元解决。
所以对串 s 中每个0 或者5 进行计算并将结果相加,即可得到最后答案。 */
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef __int64 ll;
const ll N = 1e5+;
char s[N];
const ll mod = 1e9+;
ll pw[N];
ll extgcd(ll a,ll b,ll &x,ll &y)
{
if(b==) {
x = , y = ; return a;
}
ll d = extgcd(b,a%b,x,y);
ll t = x;
x = y;
y = t-a/b*y;
return d;
}
ll inverse(ll t)
{
ll x, y;
ll d = extgcd(t,mod,x,y);
return (x%mod+mod)%mod;
}
ll Pow(ll a,ll b)
{
ll p = ;
while(b>){
if(b&){
p = p*a%mod;
}
a = a*a%mod;
b>>=;
}
return p;
}
int main()
{
ll k, p = ;
pw[] = ;
for(ll i = ; i < N; i++){
p = p*%mod;
pw[i] = p;
}
while(scanf("%s",s)!=EOF)
{
scanf("%I64d",&k);
ll len = strlen(s);
ll ans = ;
ll t = Pow(,len);
for(ll i = ; s[i]!='\0'; i++){
if(s[i]==''||s[i]=='')
ans = (ans+pw[i]*(Pow(t,k)-)%mod*inverse(t-))%mod;
}
printf("%I64d\n",ans);
}
return ;
}

C. Magic Five的更多相关文章

  1. Codeforces CF#628 Education 8 D. Magic Numbers

    D. Magic Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  2. [8.3] Magic Index

    A magic index in an array A[0...n-1] is defined to be an index such that A[i] = i. Given a sorted ar ...

  3. Python魔术方法-Magic Method

    介绍 在Python中,所有以"__"双下划线包起来的方法,都统称为"Magic Method",例如类的初始化方法 __init__ ,Python中所有的魔 ...

  4. 【Codeforces717F】Heroes of Making Magic III 线段树 + 找规律

    F. Heroes of Making Magic III time limit per test:3 seconds memory limit per test:256 megabytes inpu ...

  5. 2016中国大学生程序设计竞赛 - 网络选拔赛 C. Magic boy Bi Luo with his excited tree

    Magic boy Bi Luo with his excited tree Problem Description Bi Luo is a magic boy, he also has a migi ...

  6. 一个快速double转int的方法(利用magic number)

    代码: int i = *reinterpret_cast<int*>(&(d += 6755399441055744.0)); 知识点: 1.reinterpret_cast&l ...

  7. MAGIC XPA最新版本Magic xpa 2.4c Release Notes

    New Features, Feature Enhancements and Behavior ChangesSubforms – Behavior Change for Unsupported Ta ...

  8. Magic xpa 2.5发布 Magic xpa 2.5 Release Notes

    Magic xpa 2.5發佈 Magic xpa 2.5 Release Notes Magic xpa 2.5 Release NotesNew Features, Feature Enhance ...

  9. How Spring Boot Autoconfiguration Magic Works--转

    原文地址:https://dzone.com/articles/how-springboot-autoconfiguration-magic-works In my previous post &qu ...

  10. Magic CSS3 – 创建各种神奇的交互动画效果

    Magic CSS3 Animations 动画是一个独特的 CSS3 动画特效包,你可以自由地使用您的 Web 项目中.只需简单的在页面上引入 CSS 样式: magic.css 或者压缩版本 ma ...

随机推荐

  1. http://blog.csdn.net/zxl315/article/details/10830105

    http://blog.csdn.net/zxl315/article/details/10830105

  2. EasyUI-子页面增加显示tabs的一个问题

    在父页面点个链接能动态看到子页面的情况太简单,请看easyUI官网:http://www.jeasyui.com/tutorial/layout/tabs2.php现在说的是在子页面点个按钮也能触发增 ...

  3. Node.js 向一个文件添加内容

    最简方案: fs.appendFile('message.txt', 'data to append', function (err) { }); 参考文档: http://www.codeweblo ...

  4. Android常用传感器用法一览(1)

    1.传感器入门自从苹果公司在2007年发布第一代iPhone以来,以前看似和手机挨不着边的传感器也逐渐成为手机硬件的重要组成部分.如果读者使用过iPhone.HTC Dream.HTC Magic.H ...

  5. Android图片下载以及缓存框架

    实际开发中进行图片下载以及缓存的框架 介绍一下开发中常见图片加载框架的使用和对比一下优缺点. 1.Picasso 框架 在Android中开发,常需要从远程获取图片并显示在客户端,当然我们可以使用原生 ...

  6. LR打不开浏览器的解决方法

        很久没用LoadRunner了,今天想复习一下,免得技能生疏,安装了一个LR11,跑一下,竟然打不开IE浏览器: 这时肯定是靠谷哥跟度娘的,经过一轮搜索,可以解决打开IE了,但录制不了解决,又 ...

  7. vue vuex 大型项目demo示例

    1.vuex 动态模块配置 import Vue from 'vue' import Vuex from 'vuex' import store from '@/store'; // 使用Vuex插件 ...

  8. [XCode A] - 编程相关问题

    1.如何在XCode中新建文件夹 如果在xcode工程中new group,只是在视觉效果上分好了几个文件夹,方便分类管理,但在finder中并不会创建新的文件夹,在硬盘目录还是所有文件都并列在一个文 ...

  9. SQL中的重要语句

    1.查询每个数据库有多少空间被浪费 如果你想知道在你数据库设计后,每页有多少空间浪费掉,你可 以通过下列动态管理视图(DMV)查询下缓冲池:sys.dm_os_buffer_descriptors 从 ...

  10. Incorrect integer value: '' for column 'mid' at row 11366Incorrect integer value: '' for column 'mid' at row 1,自增字段为空,添加记录时出错

    在本地机器做测试时,数据可以正常添加,但是将代码放置到服务器上时,提示:Incorrect integer value: '' for column 'mid' at row 11366Incorre ...