1. 程式人生 > >VJ-Pasha and Phone

VJ-Pasha and Phone

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·10^k - 1 + c2·10^k - 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 10^9 + 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 < 10^k).

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 10^9 + 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.

#include<stdio.h>
long long a[100010],b[100010];
int main()
{
	int n,k;
	while(scanf("%d %d",&n,&k)!=EOF)
	{
		for(int i=0;i<n/k;i++)
		{
			scanf("%d",&a[i]);
		}
		for(int i=0;i<n/k;i++)
		{
			scanf("%d",&b[i]);
		}
		long long ans=1;
		long long f[10];                             /*pow用於double型*/ 
		f[0]=1;                      
        for(int i=1;i<10;i++)
        f[i]=f[i-1]*10;
		for(int i=0;i<n/k;i++)
		{
			long long multiple_a=(f[k]-1)/a[i]+1;        /*計算範圍內a[i]的倍數的個數,包括0,所以加1,
			                                             為什麼要減一呢,因為範圍是開區間*/
			long long multiple_0a=(f[k-1]-1)/a[i]+1;     /*計算範圍內a[i]的0倍的個數,考慮b[i]=0的情況 */ 
			long long multiple_ab=(f[k-1]*(b[i]+1)-1)/a[i]-(f[k-1]*b[i]-1)/a[i];
			                                             /*以b[i]開頭的a[i]的個數,如100範圍,a[i]=3,b[i]=4,
														此式子即50/3-40/3=3;即42,45,58這三個數字*/ 
			if(b[i]==0)
			ans*=multiple_a-multiple_0a;
			else
			ans*=multiple_a-multiple_ab;
			ans%=1000000007;
		}
		printf("%lld\n",ans);
	}
	return 0;
 } 

(還是問大佬的)
(今日はスカートを著ましたよ~)