C. Magic Five
C. Magic Five
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 524288/262144K (Java/Other)
Total Submission(s) : 12 Accepted Submission(s) : 3
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).
1
13990
2
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的更多相关文章
- 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 ...
- [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 ...
- Python魔术方法-Magic Method
介绍 在Python中,所有以"__"双下划线包起来的方法,都统称为"Magic Method",例如类的初始化方法 __init__ ,Python中所有的魔 ...
- 【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 ...
- 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 ...
- 一个快速double转int的方法(利用magic number)
代码: int i = *reinterpret_cast<int*>(&(d += 6755399441055744.0)); 知识点: 1.reinterpret_cast&l ...
- MAGIC XPA最新版本Magic xpa 2.4c Release Notes
New Features, Feature Enhancements and Behavior ChangesSubforms – Behavior Change for Unsupported Ta ...
- 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 ...
- How Spring Boot Autoconfiguration Magic Works--转
原文地址:https://dzone.com/articles/how-springboot-autoconfiguration-magic-works In my previous post &qu ...
- Magic CSS3 – 创建各种神奇的交互动画效果
Magic CSS3 Animations 动画是一个独特的 CSS3 动画特效包,你可以自由地使用您的 Web 项目中.只需简单的在页面上引入 CSS 样式: magic.css 或者压缩版本 ma ...
随机推荐
- 使用虚拟机运行Ubuntu时,主机与宿主机共享文件的方法。
简介: 首先设置虚拟机: 虚拟机 -> 设置-> Hardware -> Network Adapter,在网络连接处设置为 “桥接:直接连接到物理网络”,“NAT:使用已共享的主机 ...
- 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-如何修改代码字体
工具-选项 TwinCAT,PLC Environment,Text editor,然后在文本区域中修改字体 更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: http://i. ...
- 数据採集器服务——Socket(今天才发现AES加解密代码跟贴的时候不一样,貌似乱码,不知什么情况)
近期刚做的一个项目.关于 Socket TCP 通信. 需求方提供了一个 ARM 机器,及数据採集器,须要我做一个服务端与数据採集器进行交互. 目的: 数据採集器:定时将读取到的数据发送到服务端. 服 ...
- 增强for循环、Map接口遍历、可变參数方法
增强for循环 1.for循环能做得事情.增强for循环大部分都能做(假设要想获得下标的时候就必须使用简单for循环了) 2.增强for有时候可以方便的处理集合遍历的问题,可是集合的标准遍历是使用迭代 ...
- Mybatis <where>标签
<select id="findActiveBlogLike" resultType="Blog"> SELECT * FROM BLOG WHER ...
- Getting started with Chrome Dev Editor
转自:https://github.com/GoogleChrome/chromedeveditor/blob/master/doc/GettingStarted.md Installation In ...
- C-Scanf连续调用多次并且存在%c的问题
问题现象: 当程序中存在多个scanf时,针对第一个scanf的输入,一般用户会以空白字符(空格.换行.tab.换页符)等结束.但若后面有一个scanf(“%c”,&ch),则刚才输入的空白字 ...
- PHP 导出 CSV 文件用 Excel 打开出现中文乱码
本篇文章由:http://xinpure.com/php-export-csv-file-opened-by-excel-appear-garbled/ 乱码情况 写了一段导出 CSV 文件的代码,可 ...
- HTML5学习笔记1 HTML5 新元素
自1999年以后html4.0已经改变了很我,今天,在html4.01中的几个已经被废弃,这些元素在html5中已经被删除或重新定义. 为了更好地处理今天的互联网应用,html5添加了很多新元素及功能 ...
- 使用 scikit-learn 实现多类别及多标签分类算法
多标签分类格式 对于多标签分类问题而言,一个样本可能同时属于多个类别.如一个新闻属于多个话题.这种情况下,因变量yy需要使用一个矩阵表达出来. 而多类别分类指的是y的可能取值大于2,但是y所属类别是唯 ...