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)

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdio>
  4. #include<cmath>
  5. #include<algorithm>
  6. #include<cstring>
  7. #define N 1000005
  8. #define MOD 1000000007
  9. using namespace std;
  10. typedef __int64 LL;
  11. int a[N];
  12. int b[N];
  13. int f[];
  14. void init(){
  15. f[] = ;
  16. for(LL i=; i<; ++i)
  17. f[i] = f[i-] * ;
  18. }
  19.  
  20. int main(){
  21. init();
  22. int n, k;
  23. scanf("%d%d", &n, &k);
  24. int m = n/k;
  25. for(int i=; i<=m; ++i)
  26. scanf("%d", &a[i]);
  27. for(int i=; i<=m; ++i)
  28. scanf("%d", &b[i]);
  29. LL ans = ;
  30. for(int i=; i<=m; ++i){
  31. LL tot = (f[k]-)/a[i] + ;
  32. LL bi_tot0 = (f[k-]-)/a[i] + ;//block的开头是0, 即b[i]==0
  33. LL bi_tot = (f[k-]*(b[i]+)-)/a[i] - (f[k-]*b[i]-)/a[i];//block的开头不是0
  34. if(b[i] == )
  35. ans *= tot-bi_tot0;
  36. else
  37. ans *= tot-bi_tot;
  38. ans %= MOD;
  39. }
  40. printf("%I64d\n", ans);
  41. return ;
  42. }
                        C. Duff and Weight Lifting
 

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.

Input

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.

Output

Print the minimum number of steps in a single line.

Sample test(s)
input
  1. 5
    1 1 2 3 3
output
  1. 2
input
  1. 4
    0 1 2 3
output
  1. 4
Note

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 的个数就是答案!

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cmath>
  5. #include<map>
  6. using namespace std;
  7. map<int, int, less<int> >mp;
  8. int main(){
  9. int n;
  10. scanf("%d", &n);
  11. while(n--){
  12. int x;
  13. scanf("%d", &x);
  14. mp[x]++;
  15. }
  16. int ans = ;
  17. for(map<int,int>::iterator it = mp.begin(); it!=mp.end(); ++it){
  18. if(it->second>)
  19. mp[it->first + ] += it->second / ;
  20. if(it->second% != ) ++ans;
  21. }
  22. printf("%d\n", ans);
  23. return ;
  24. }

Codeforces Round #326 (Div. 2) B. Pasha and Phone C. Duff and Weight Lifting的更多相关文章

  1. 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 ...

  2. 字符串处理 Codeforces Round #297 (Div. 2) B. Pasha and String

    题目传送门 /* 题意:给出m个位置,每次把[p,len-p+1]内的字符子串反转,输出最后的结果 字符串处理:朴素的方法超时,想到结果要么是反转要么没有反转,所以记录 每个转换的次数,把每次要反转的 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. matlab 求解线性方程组之范数

    1.赋范线性空间和内积空间 在线性代数的初级教材里,一般是在向量空间中定义内积,然后再由内积来导出范数,比如在n维实向量空间中: |x||=√<x,x> 在线性代数的高级教材中,一般是将内 ...

  2. 【异常】INFO: TopologyManager: EndpointListener changed ...

    5月份做云部署,在调试CSS系统时,出现启动系统时,卡死情况,后台日志如下: May 03, 2016 2:34:52 AM org.apache.cxf.dosgi.topologymanager. ...

  3. 在线图片压缩后以ImageIO 流的形式 设置大小显示指定页面

    1.Servlet   代码 public class ZoomImgServlet extends HttpServlet implements Servlet { public void init ...

  4. Red Hat5下源码安装mysql5.6过程记录

    1.安装cmake包 [root@edu soft]# tar -xzf cmake-.tar.Z [root@edu soft]# cd cmake- [root@edu cmake-]# ./co ...

  5. mysql 命令行还原备份数据库

    通常数据库还原备份可以通过navicat等数据库管理工具进行,只需要简单的导出导入就行了,但遇到有索引外键的数据库,数据库管理工具运行.sql文件会报错,这时候可以尝试命令行导入,亲测可以成功 MyS ...

  6. 初识 Html5

    1.1认识HTML5 HTML5并不仅仅只是做为HTML标记语言的一个最新版本,更重要的是它制定了Web应用开发的一系列标准,成为第一个将Web做为应用开发平台的HTML语言. HTML5定义了一系列 ...

  7. CYQ.Data V5 MDataTable 专属篇介绍

    前言 以前一两个月才出一篇,这三天有点变态地连续1天1篇(其实都是上周末两天写好的存货). 短期应该没有新的和此框架相关的文章要写了,这应该是最后一篇,大伙且看且珍惜. 前两篇讲数据库读写分离和分布式 ...

  8. IT人生知识分享:概率与运气

    前言: 最近的人生多了些体验,也读了些许书,感觉还是有些知识是可以分享的. 今天难得周六,特意开电脑了,花几个小时写写,和大伙分享分享点知识. 以下内容,更多的需要读者思考,所以结论不会写太清晰,但一 ...

  9. JVM 备注

    一.堆内存分布: JAVA 分为堆内存和栈内存,GC主要针对堆内存 1)Young: 存放新生内存对象 1.1)Eden JVM 刚开始分配的对象 1.2)Survivor1(from) 1.3)Su ...

  10. 辛巴学院-Unity-剑英陪你零基础学c#系列(四)函数和封装

    辛巴学院:正大光明的不务正业. 国庆长假结束了,我的心情是这样的: 你总是起不早,起不早独自一个人沉睡到天亮你无怨无悔的梦着那副本我知道你根本就不想上班你总是起不早,起不早放假总是短暂,上班太难请个病 ...