1. 程式人生 > >W - Pasha and Phone CodeForces - 595B (收益頗豐的數學題

W - Pasha and Phone CodeForces - 595B (收益頗豐的數學題

 

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, ..., k and so on. Pasha considers a phone number good, if the i-th block doesn't start from the digit b
i 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

自己英語水平捉雞,這道題目看了半天硬是沒看懂

大意就是說有n為一串數字的長度,k為把一串數字分組後每一組中數字的個數
又有a【i】b【i】使得數字串滿足 ,每一組為a【i】的倍數,且不以b【i】開頭;

這是我開始寫的,對於b【i】不能開頭,我是遍歷判斷的.....(不要打我,我只會這個)
結果自然不行
#include<stdio.h>
#include<math.h>
int main(){
    long long undo=1;
    long long a[100000],b[100000],s=0;
    int i,j,n,k,d,mod;
     while(scanf("%d%d",&n,&k)!=EOF){
    d=pow(10,k)-1;mod=pow(10,k-1);
    for(i=0;i<n/k;i++)scanf("%I64d",&a[i]);
    for(i=0;i<n/k;i++)scanf("%I64d",&b[i]);
    for(i=0;i<n/k;i++){
        s+=d/a[i]+1;
        for(j=0;j*a[i]<=d;j++){         //最多9個數量級迴圈判斷,我怕不是個傻子 
            if(j*a[i]/mod==b[i])s--;
             }
             undo*=s;
             undo%=1000000007;
             s=0;
    }
    printf("%I64d\n",undo%1000000007);
    undo=1;
    }
    return 0;
}

 看了大神的程式碼,直接用算式算出不滿足要求2的數字個數,在用總數減去...

又想起新生賽中一道簽到題,等差數列求和,我用了迴圈,答案是直接套公式算,速度肯定快

所以做題時可以直接用算式算出來的,就儘量不要用迴圈獲其他的算,大道至簡

於是改之後...還是wa了 ,而且各種除錯,比較結果都找不出錯

百度之,發現是pow()的問題,這東西精度不夠,而且裡面都是double型,做題就不要用

函式原型:double pow( double x, double y );

pow

標頭檔案:math.h/cmath(C++中)

功能:計算x的y次方

返回值:x不能為負數且y為小數,或者x為0且y小於等於0,返回冪指數的結果。

 

ac的程式碼:

 1 #include<stdio.h>
 2 #include<math.h>
 3 long long power(int k){
 4     int s=1;
 5     while(k--)s*=10;
 6     return s;
 7 } 
 8 int main(){
 9     long long undo=1;
10     long long a[100000],b[100000],s=0;
11     int i,j,n,k,d,mod;
12      while(scanf("%d%d",&n,&k)!=EOF){
13          d=power(k); mod=d/10;
14     for(i=0;i<n/k;i++)scanf("%lld",&a[i]);
15     for(i=0;i<n/k;i++)scanf("%lld",&b[i]);
16     for(i=0;i<n/k;i++){
17         s+=(d-1)/a[i]+1;         //帶零,所有a[i]在該位數下的倍數個數 
18         if(b[i]!=0)
19         s-=((b[i]+1)*mod-1)/a[i]-(b[i]*mod-1)/a[i];  //減去10*b[i]~10*(b[i]+1)-1 內ai倍數的個數 eg;30~39                     
20         else if(b[i]==0)
21         s-=(mod-1)/a[i]+1;                   //如果bi是0,減去所有0頭的ai倍數 
22              undo*=s;
23              undo%=1000000007;
24              s=0;
25     }
26     printf("%lld\n",undo);
27     undo=1;
28     }
29     return 0;
30 }

總之:1.能用式子解決的問題,就不要用迴圈獲判斷來解決

           2.能不用pow(),就不用pow()。