傳送門:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=470&problem=1979
題意:求[m,n]中有多少個0
題解:分位元分析:
n的第i位不為0=n左邊的數(高位)*10^(i-1)
n的第i位為0 =(n左邊的數-1)*10^(i-1)+(i位右邊的數+1)
ans=f(n)-f(m-1);
注意m為0的情況即可。
AC代碼:
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <cstdlib>#include <cmath>#include <vector>#include <list>#include <deque>#include <queue>#include <iterator>#include <stack>#include <map>#include <set>#include <algorithm>#include <cctype>using namespace std;#define si1(a) scanf("%d",&a)#define si2(a,b) scanf("%d%d",&a,&b)#define sd1(a) scanf("%lf",&a)#define sd2(a,b) scanf("%lf%lf",&a,&b)#define ss1(s) scanf("%s",s)#define pi1(a) printf("%d\n",a)#define pi2(a,b) printf("%d %d\n",a,b)#define mset(a,b) memset(a,b,sizeof(a))#define forb(i,a,b) for(int i=a;i<b;i++)#define ford(i,a,b) for(int i=a;i<=b;i++)typedef long long LL;const int N=110001;const int M=1000007;const int INF=0x3f3f3f3f;const double PI=acos(-1.0);const double eps=1e-7;LL xiaohao(LL l){ LL sum=0,j=1,r=0,m; while(l) { m=l%10; l/=10; if(m) sum+=l*j; else sum+=(l-1)*j+r+1; r=r+m*j; j=j*10; } return sum;}int main(){ LL n,m; while(scanf("%lld%lld",&m,&n)&&m>=0) { LL x=xiaohao(n)-xiaohao(m-1); if(m==0) x++; printf("%lld\n",x); } return 0;}