Codeforces Round #326 (Div. 2) B. Pasha and Phone C. Duff and Weight Lifting
B. Pasha and Phone
Pasha has recently bought a new phone jPager and started adding his friends' phone numbers there. Each phone number consists of exactly n digits.
Also Pasha has a number k and two sequences of length n / k (n is divisible by k) a1, a2, ..., an / k and b1, b2, ..., bn / k. Let's split the phone number into blocks of length k. The first block will be formed by digits from the phone number that are on positions 1, 2,..., k, the second block will be formed by digits from the phone number that are on positions k + 1, k + 2, ..., 2·k and so on. Pasha considers a phone number good, if the i-th block doesn't start from the digit bi and is divisible by ai if represented as an integer.
To represent the block of length k as an integer, let's write it out as a sequence c1, c2,...,ck. Then the integer is calculated as the result of the expression c1·10k - 1 + c2·10k - 2 + ... + ck.
Pasha asks you to calculate the number of good phone numbers of length n, for the given k, ai and bi. As this number can be too big, print it modulo 109 + 7.
Input
The first line of the input contains two integers n and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ min(n, 9)) — the length of all phone numbers and the length of each block, respectively. It is guaranteed that n is divisible by k.
The second line of the input contains n / k space-separated positive integers — sequence a1, a2, ..., an / k (1 ≤ ai < 10k).
The third line of the input contains n / k space-separated positive integers — sequence b1, b2, ..., bn / k (0 ≤ bi ≤ 9).
Output
Print a single integer — the number of good phone numbers of length n modulo 109 + 7.
Sample test(s)
input
6 2
38 56 49
7 3 4
output
8
input
8 2
1 22 3 44
5 4 3 2
output
32400
Note
In the first test sample good phone numbers are: 000000, 000098, 005600, 005698, 380000, 380098, 385600, 385698.
题意:将一个长度为n的phone number,分成长度为k的n/k给块,如果任意一个块(块i)中的数字x的开头数字不是b[i], 且x可以整除a[i],那么就称这个phone number是 good number。问这样的good number 有多少个!
思路:容斥原理。 块 i : tot = 数字长度为k且可以整除a[i]的个数;
bi_tot = 数字长度为k且开头数字为b[i]且可以整除a[i]的个数 - 数字长度为k且开头数字为(b[i]-1)且可以整除a[i]的个数
那么符合要求的数字个数 = tot - bi_tot;(b[i]>0)
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#define N 1000005
#define MOD 1000000007
using namespace std;
typedef __int64 LL;
int a[N];
int b[N];
int f[];
void init(){
f[] = ;
for(LL i=; i<; ++i)
f[i] = f[i-] * ;
} int main(){
init();
int n, k;
scanf("%d%d", &n, &k);
int m = n/k;
for(int i=; i<=m; ++i)
scanf("%d", &a[i]);
for(int i=; i<=m; ++i)
scanf("%d", &b[i]);
LL ans = ;
for(int i=; i<=m; ++i){
LL tot = (f[k]-)/a[i] + ;
LL bi_tot0 = (f[k-]-)/a[i] + ;//block的开头是0, 即b[i]==0
LL bi_tot = (f[k-]*(b[i]+)-)/a[i] - (f[k-]*b[i]-)/a[i];//block的开头不是0
if(b[i] == )
ans *= tot-bi_tot0;
else
ans *= tot-bi_tot;
ans %= MOD;
}
printf("%I64d\n", ans);
return ;
}
Recently, Duff has been practicing weight lifting. As a hard practice, Malek gave her a task. He gave her a sequence of weights. Weight of i-th of them is 2^wi pounds. In each step, Duff can lift some of the remaining weights and throw them away. She does this until there's no more weight left. Malek asked her to minimize the number of steps.
Duff is a competitive programming fan. That's why in each step, she can only lift and throw away a sequence of weights 2^a1, ..., 2^ak if and only if there exists a non-negative integer x such that 2^a1 + 2^a2 + ... + 2^ak = 2^x, i. e. the sum of those numbers is a power of two.
Duff is a competitive programming fan, but not a programmer. That's why she asked for your help. Help her minimize the number of steps.
The first line of input contains integer n (1 ≤ n ≤ 106), the number of weights.
The second line contains n integers w1, ..., wn separated by spaces (0 ≤ wi ≤ 106 for each 1 ≤ i ≤ n), the powers of two forming the weights values.
Print the minimum number of steps in a single line.
5
1 1 2 3 3
2
4
0 1 2 3
4
In the first sample case: One optimal way would be to throw away the first three in the first step and the rest in the second step. Also, it's not possible to do it in one step because their sum is not a power of two.
In the second sample case: The only optimal way is to throw away one weight in each step. It's not possible to do it in less than 4 steps because there's no subset of weights with more than one weight and sum equal to a power of two.
题意:给定n个数w1, w2, w3,.......wn, 然后从这个n数中找出这样的一个子序列a1,a2,a3....ak 使得 2^a1+2^a2.....+2^ak = 2^x, 然后可以删除这个序列,
那么最少经过几步可以全部将这n个数删除!
思路:其实就是 二进制数 相加的过程,n个数对应n个二进制数,从最低位到最高位相加,得到最后的二进制数中 1 的个数就是答案!
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
using namespace std;
map<int, int, less<int> >mp;
int main(){
int n;
scanf("%d", &n);
while(n--){
int x;
scanf("%d", &x);
mp[x]++;
}
int ans = ;
for(map<int,int>::iterator it = mp.begin(); it!=mp.end(); ++it){
if(it->second>)
mp[it->first + ] += it->second / ;
if(it->second% != ) ++ans;
}
printf("%d\n", ans);
return ;
}
Codeforces Round #326 (Div. 2) B. Pasha and Phone C. Duff and Weight Lifting的更多相关文章
- Codeforces Round #297 (Div. 2)B. Pasha and String 前缀和
Codeforces Round #297 (Div. 2)B. Pasha and String Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx ...
- 字符串处理 Codeforces Round #297 (Div. 2) B. Pasha and String
题目传送门 /* 题意:给出m个位置,每次把[p,len-p+1]内的字符子串反转,输出最后的结果 字符串处理:朴素的方法超时,想到结果要么是反转要么没有反转,所以记录 每个转换的次数,把每次要反转的 ...
- Codeforces Round #326 (Div. 2) C. Duff and Weight Lifting 水题
C. Duff and Weight Lifting Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...
- Codeforces Round #337 (Div. 2) A. Pasha and Stick 数学
A. Pasha and Stick 题目连接: http://www.codeforces.com/contest/610/problem/A Description Pasha has a woo ...
- Codeforces Round #311 (Div. 2)B. Pasha and Tea 水题
B. Pasha and Tea Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/557/prob ...
- Codeforces Round #330 (Div. 2) B. Pasha and Phone 容斥定理
B. Pasha and Phone Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/595/pr ...
- Codeforces Round #337 (Div. 2) A. Pasha and Stick 水题
A. Pasha and Stick Pasha has a wooden stick of some positive integer length n. He wants to perform ...
- Codeforces Round #326 (Div. 2) D. Duff in Beach dp
D. Duff in Beach Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/588/probl ...
- Codeforces Round #326 (Div. 2) B. Duff in Love 分解质因数
B. Duff in Love Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/588/proble ...
随机推荐
- 用soapUI测试webservice
测试webservice时,有时需要写一个客户端来向服务端发起请求才可以测试服务,最近看到一款工具soap ui,也可以调试VS2010中的程序. 首先要把webservice 发布到本地,网上已经有 ...
- 关于call和apply函数的区别及用法
call和apply函数是function函数的基本属性,都可以用于更改函数对象和传递参数,是前端工程师常用的函数.具体使用方法请参考以下案列: 例如: 申明函数: var fn = function ...
- sql编程篇 (五) 事务
计算机中的事务 编辑 概念 事务(Transaction)是访问并可能更新数据库中各种数据项的一个程序执行单元(unit).事务通常由高级数据库操纵语言或编程语言(如SQL,C++或Java)书写的用 ...
- wamp2.5 局域网无法访问问题
1.打开http.conf文件,在对应处修改为如下内容(通常经过步骤一之后就能访问了,若不行则再执行后面步骤) <Directory /> Options FollowSymLinks A ...
- The easy way to implement a Red-Black tree
Red-Black trees are notorious for being nightmares of pointer manipulation. Instructors will show th ...
- 《HiWind企业快速开发框架实战》(1)框架的工作原理
<HiWind企业快速开发框架实战>(1)框架的工作原理 1.HiWind架构 HiWind的基本架构如下: 持久层部分:同时为框架本身的业务服务,也为开发人员的自定义业务服务. 逻辑层: ...
- WPF+通过配置文件生成菜单(Menu)+源码
这个月做项目,遇到过一个通过配置文件来生成菜单的解决方案,感觉挺优雅的,特地放到博客园来,以飨读者. 说来惭愧,以前做的项目都没有这样用过,都是固定死了.如果后续有需要加入菜单,还得在重新修改UI,然 ...
- C#+OpenGL+FreeType显示3D文字(2) - 用GLSL+VBO绘制文字
C#+OpenGL+FreeType显示3D文字(2) - 用GLSL+VBO绘制文字 +BIT祝威+悄悄在此留下版了个权的信息说: 上一篇得到了字形贴图及其位置字典(可导出为XML).本篇就利用此贴 ...
- PHP运行及语句及逻辑
php开发网页需要存放在wamp根目录下的www文件夹中才可运行成功.同时wamp要处于运行状态. 无站点情况下打开方式: 网址栏中输入:localhost/文件名称 代码规范: 用 <?p ...
- Nginx服务器之 Nginx的基本配置
本文使用 Linux centos系统 一.Nginx虚拟主机的配置 虚拟主机:通常情况下,为了使每个服务器可以供更多用户使用,可以将一个服务器分为很多虚拟的子服务器,每个子服务器都是互相独立的.这些 ...