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 ...
随机推荐
- python 实现计算数独
输入文件格式: 008309100900060004007504800036000540001000600042000970005907300600010008004608200 输出结果: yuan ...
- 新型数据库Kudu应用经验分享
小米使用kudu的案例 http://www.aiweibang.com/yuedu/60603532.html 调研kudu的情况
- Sqlite-Sqlite3中的数据类型
大多数的数据库引擎(到现在据我们所知的除了sqlite的每个sql数据库引擎)都使用静态的.刚性的类型,使用静态类型,数据的类型就由它的容器决定,这个容器是这个指被存放的特定列. Sqlite使用一个 ...
- (转)NIO 文件锁定
文件锁定 概述 文件锁定初看起来可能让人迷惑.它 似乎 指的是防止程序或者用户访问特定文件.事实上,文件锁就像常规的 Java 对象锁 ― 它们是 劝告式的(advisory) 锁.它们不阻止任何形式 ...
- XP系统如何把桌面图标变大
右击桌面,属性,外观,高级,在项目里面找到图标,大小改为你喜欢的样式. 我测试的结果是:图标大小改为42,字体大小改为8,图标垂直间距改为100,水平间距改为54效果不错.
- windows域相关
查看域角色: netdom query fsmo
- H5 Canvas | 画布中变量作用域与setInterval方法同步执行探究
Demo - 随机绘制圆环 实现思路: 将一个圆环的绘制分成100份,setInterval()方法定义每隔时间n绘制一段新的,每份的开始路径都是上一次的结束路径,实现步进绘制. 通过Math.ran ...
- Mybatis <where>标签
<select id="findActiveBlogLike" resultType="Blog"> SELECT * FROM BLOG WHER ...
- 转:浅析VO、DTO、DO、PO的概念、区别和用处
原文链接 概念: VO(View Object):视图对象,用于展示层,它的作用是把某个指定页面(或组件)的所有数据封装起来. DTO(Data Transfer Object):数据传输对象,这个概 ...
- Lintcode---区间最小数
给定一个整数数组(下标由 0 到 n-1,其中 n 表示数组的规模),以及一个查询列表.每一个查询列表有两个整数 [start, end]. 对于每个查询,计算出数组中从下标 start 到 end ...