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 ...
随机推荐
- http://m.blog.csdn.net/article/details?id=2630620
http://m.blog.csdn.net/article/details?id=2630620
- js 中小数取整的函数
1.丢弃小数部分,保留整数部分 js:parseInt(7/2) 2.向上取整,有小数就整数部分加1 js: Math.ceil(7/2) 3,四舍五入. js: Math.round(7/2) 4, ...
- NIO的简单Demo
package jesse.test1; import java.io.IOException; import java.net.InetAddress; import java.net.InetSo ...
- Win7开机提示group policy client无法登陆怎么办
1 开机按F8,进入安全模式 2 进入系统之后运行注册表,定位到HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Prof ...
- Spring-1 之入门
(一)简单对象Spring XML配置说明 使用Spring (Spring 3.0) 实现最简单的类映射以及引用,属性赋值: 1.1.新建类UserModel: 1 2 3 4 5 6 7 8 9 ...
- python求pi的方法
来自 #_*_ coding=utf-8 *_* ## {{{ http://code.activestate.com/recipes/578130/ (r5) def pi(places=10): ...
- CXF 调用方式——动态创建客户端(调用稳定版本号为2.7.18)
今天用动态创建客户端的方式调用webservice,报了这样一个错: 2017-01-05 20:51:46,029 DEBUG main org.apache.cxf.common.logging. ...
- Android Touch事件传递机制引发的血案
尊重原创:http://blog.csdn.net/yuanzeyao/article/details/38942135 关于Android Touch事件传递机制我之前也写过两篇文章,自觉得对Tou ...
- linux磁盘满时,如何定位并删除文件
原文链接: http://www.cnblogs.com/yinxiangpei/articles/4211743.html @1.一般情况 一般情况下先df看一下,然后cd到要满的盘,执行: d ...
- 用Fiddler 发送post请求
在调试web api的时候,若是get 请求,可以直接在浏览器里查看结果,如果是put,或者post请求在浏览器地址栏里就没有办法了. 下面介绍一下,如何利用fiddler模拟post请求. 也可以用 ...