[VJ][數學]Pasha and Phone
Pasha and Phone
Description
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
To represent the block of length k as an integer, let's write it out as a sequence c
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.
描述:
有一個整數,整數的位數是n,把整數分為 n/k 塊,保證 n/k 是整數。
總共有1—n/k 塊,要使第 i 塊是a[i]的倍數(包括0倍數),並且第一位不是b[i]。
求這樣的整數有多少個。
分析:
剛開始寫列舉每一塊,使那一塊的整數是a[i]的倍數,並且第一位不是b[i]。
有排列組合可知,答案就是每一塊的個數相乘。
後來TLE了。
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<algorithm> 5 #include<string> 6 #include<cstring> 7 using namespace std; 8 long long ans=1,maxx=1; 9 long long a[100010]; 10 int b[100010]; 11 int main() 12 { 13 int n,k; 14 cin>>n>>k; 15 for(int i=1;i<=n/k;i++) 16 cin>>a[i]; 17 for(int i=1;i<=n/k;i++) 18 cin>>b[i]; 19 for(int i=1;i<k;i++) 20 maxx*=10; 21 for(int i=1;i<=n/k;i++) 22 { 23 long long cnt=0; 24 for(int j=0; ;j++) 25 { 26 if(a[i]*j>=maxx*10) break; 27 else{ 28 if(a[i]*j/maxx!=b[i]) 29 cnt++; 30 else continue; 31 } 32 } 33 ans=ans*cnt; 34 ans=ans%1000000007; 35 } 36 cout<<ans<<endl; 37 return 0; 38 }View Code
正確解法:
列舉每一塊,每一塊的最大數/a[i] 就是除了0倍數之外的倍數的個數。