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, ..., 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
6 2
38 56 49
7 3 4
8
8 2
1 22 3 44
5 4 3 2
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()。