Codeforces 595 B. Pasha and Phone (容斥)
2016-09-29 11:22
357 查看
题目连接:http://codeforces.com/contest/595/problem/B
B. Pasha and Phone
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Examples
input
output
input
output
Note
In the first test sample good phone numbers are: 000000, 000098, 005600, 005698, 380000, 380098, 385600, 385698.
题目大意:
给你ai和bi,让你找到有多少个k位数,使得这个k位数不以bi开头且mod ai=0
处理n/k次,然后把所有的答案都乘起来。
解题思路:mid ai=0 的方案数减去bi开头的方案数。对bi=0的情况分类讨论。
/* ***********************************************
┆ ┏┓ ┏┓ ┆
┆┏┛┻━━━┛┻┓ ┆
┆┃ ┃ ┆
┆┃ ━ ┃ ┆
┆┃ ┳┛ ┗┳ ┃ ┆
┆┃ ┃ ┆
┆┃ ┻ ┃ ┆
┆┗━┓ 马 ┏━┛ ┆
┆ ┃ 勒 ┃ ┆
┆ ┃ 戈 ┗━━━┓ ┆
┆ ┃ 壁 ┣┓┆
┆ ┃ 的草泥马 ┏┛┆
┆ ┗┓┓┏━┳┓┏┛ ┆
┆ ┃┫┫ ┃┫┫ ┆
┆ ┗┻┛ ┗┻┛ ┆
************************************************ */
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <bitset>
using namespace std;
#define rep(i,a,b) for (int i=(a),_ed=(b);i<=_ed;i++)
#define per(i,a,b) for (int i=(b),_ed=(a);i>=_ed;i--)
#define pb push_back
#define mp make_pair
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 1000000007
#define LL long long
#define ULL unsigned long long
#define MS0(X) memset((X), 0, sizeof((X)))
#define SelfType int
SelfType Gcd(SelfType p,SelfType q){return q==0?p:Gcd(q,p%q);}
SelfType Pow(SelfType p,SelfType q){SelfType ans=1;while(q){if(q&1)ans=ans*p;p=p*p;q>>=1;}return ans;}
#define Sd(X) int (X); scanf("%d", &X)
#define Sdd(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define Sddd(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin())
#define all(a) a.begin(), a.end()
#define mem(x,v) memset(x,v,sizeof(x))
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<long long> vll;
inline LL read(){LL ra,fh;char rx;rx=getchar(),ra=0,fh=1;while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
LL a[100005];
LL b[100005];
LL f[10];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);
int n,k;
n = read(),k = read();
f[0] = 1;
for(int i=1;i<=n/k;i++) a[i] = read();
for(int i=1;i<=n/k;i++) b[i] = read();
for(int i=1;i<=10;i++) f[i] = f[i-1]*10;
LL ans = 1;
for(int i=1;i<=n/k;i++)
{
LL t1 = (f[k]-1)/a[i] + 1; //是a[i]的倍数的数的个数。+1是全是0的情况
LL t2 = (f[k-1]-1)/a[i] + 1; //是a[i]的0倍的数的个数。
LL t3 = (f[k-1]*(b[i]+1)-1)/a[i] - (f[k-1]*b[i]-1)/a[i]; //是a[i]的倍数并且包含b[i]的数的个数
if(b[i]==0) ans *= t1 - t2;
else ans *= t1 - t3;
ans %= mod;
}
printf("%I64d\n",ans);
return 0;
}
B. Pasha and Phone
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Examples
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.
题目大意:
给你ai和bi,让你找到有多少个k位数,使得这个k位数不以bi开头且mod ai=0
处理n/k次,然后把所有的答案都乘起来。
解题思路:mid ai=0 的方案数减去bi开头的方案数。对bi=0的情况分类讨论。
/* ***********************************************
┆ ┏┓ ┏┓ ┆
┆┏┛┻━━━┛┻┓ ┆
┆┃ ┃ ┆
┆┃ ━ ┃ ┆
┆┃ ┳┛ ┗┳ ┃ ┆
┆┃ ┃ ┆
┆┃ ┻ ┃ ┆
┆┗━┓ 马 ┏━┛ ┆
┆ ┃ 勒 ┃ ┆
┆ ┃ 戈 ┗━━━┓ ┆
┆ ┃ 壁 ┣┓┆
┆ ┃ 的草泥马 ┏┛┆
┆ ┗┓┓┏━┳┓┏┛ ┆
┆ ┃┫┫ ┃┫┫ ┆
┆ ┗┻┛ ┗┻┛ ┆
************************************************ */
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <bitset>
using namespace std;
#define rep(i,a,b) for (int i=(a),_ed=(b);i<=_ed;i++)
#define per(i,a,b) for (int i=(b),_ed=(a);i>=_ed;i--)
#define pb push_back
#define mp make_pair
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 1000000007
#define LL long long
#define ULL unsigned long long
#define MS0(X) memset((X), 0, sizeof((X)))
#define SelfType int
SelfType Gcd(SelfType p,SelfType q){return q==0?p:Gcd(q,p%q);}
SelfType Pow(SelfType p,SelfType q){SelfType ans=1;while(q){if(q&1)ans=ans*p;p=p*p;q>>=1;}return ans;}
#define Sd(X) int (X); scanf("%d", &X)
#define Sdd(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define Sddd(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin())
#define all(a) a.begin(), a.end()
#define mem(x,v) memset(x,v,sizeof(x))
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<long long> vll;
inline LL read(){LL ra,fh;char rx;rx=getchar(),ra=0,fh=1;while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
LL a[100005];
LL b[100005];
LL f[10];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);
int n,k;
n = read(),k = read();
f[0] = 1;
for(int i=1;i<=n/k;i++) a[i] = read();
for(int i=1;i<=n/k;i++) b[i] = read();
for(int i=1;i<=10;i++) f[i] = f[i-1]*10;
LL ans = 1;
for(int i=1;i<=n/k;i++)
{
LL t1 = (f[k]-1)/a[i] + 1; //是a[i]的倍数的数的个数。+1是全是0的情况
LL t2 = (f[k-1]-1)/a[i] + 1; //是a[i]的0倍的数的个数。
LL t3 = (f[k-1]*(b[i]+1)-1)/a[i] - (f[k-1]*b[i]-1)/a[i]; //是a[i]的倍数并且包含b[i]的数的个数
if(b[i]==0) ans *= t1 - t2;
else ans *= t1 - t3;
ans %= mod;
}
printf("%I64d\n",ans);
return 0;
}
相关文章推荐
- CodeForces 595B Pasha and Phone
- Codeforces 595 B. Pasha and Phone【数学】
- codeforces 300 div2 B.Pasha and Phone 容斥原理
- Codeforces Round #330 (Div. 2) B. Pasha and Phone 容斥定理
- CodeForces 595B Pasha and Phone
- codeforces-330-Pasha and Phone(容差定理)
- Codeforces Round #330 (Div. 2) B. Pasha and Phone 容斥定理
- codeforces 689A A. Mike and Cellphone(水题)
- 【Codeforces】547C Mike and Foam 容斥
- CodeForces 689A -Mike and Cellphone
- codeforces 508 A Pasha and Pixels
- codeforces 449D - Jzzhu and Numbers 高维前缀和 容斥
- codeforces 361 A - Mike and Cellphone
- Codeforces 548E Mike and Foam(容斥)
- Codeforces 547C Mike and Foam 容斥
- 【CodeForces 557B】Pasha and Tea
- codeforces 595 D. Max and Bike
- Codeforces 547C - Mike and Foam (容斥)
- codeforces 610A Pasha and Stick
- codeforces 595 E. Edo and Magnets