| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1322 人关注过本帖
标题:一个数位dp的题目
只看楼主 加入收藏
a1004573547
Rank: 2
等 级:论坛游民
帖 子:78
专家分:25
注 册:2013-3-11
收藏
得分:0 
我把三种情况分开了。。。
好难写啊  特别是对于p是两位的 并且十位和个位不一样的情况
2014-01-24 12:32
a1004573547
Rank: 2
等 级:论坛游民
帖 子:78
专家分:25
注 册:2013-3-11
收藏
得分:0 
程序代码:
#include <iostream>
#include <algorithm>
using namespace std;
long long count2(int n,int p);
long long count3(int n,int p);
long long l,r,k;
long long b[]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000,10000000000LL};
long long c[]={0,9,99,999,9999,99999,999999,9999999,99999999,999999999LL,9999999999LL,99999999999LL};

long long count1(int n,int p)
{   if(n<0) return 0;
if(n<10) {

    return 1+n-(n>=p);
}

    int i=0;
     while(b[i+1]<n) i++;
     int a=n/b[i];
     if(a==p){
        return count1(n-b[i]+c[i],p);

     }
     else if(a>p) {

        return (n/b[i]-1)*count1(c[i],p)+count1(n%b[i],p);

     }
     else{
       return (n/b[i])*count1(c[i],p)+count1(n%b[i],p);

     }

}
long long f2(int n,int a2,int d,int p)
{

    if(n/b[d]==0&&a2==0) return 0;
    if(n<10) return n+1-(n>=a2);
    int m=n/b[d];
    if(m==0){
        return count2(n,p);
    }
    else{
    if(m>a2)
    {  return (m-1)*count2(c[d],p)+count2(n%b[d],p);

    }else if(m==a2)
    {  return (m)*count2(c[d],p);

    }else{
        return m*count2(c[d],p)+count2(n%b[d],p);

    }

    }


}
long long count2(int n,int p)
{
    if(n<p) return 0;
    if(n<100) return n+1-(n>=p);
    int a=p/10;
    int a2=p%10;
     int i=0;
     while(b[i+1]<n) i++;


    if(n/b[i]>a) {
        return (n/b[i]-1)*count2(c[i],p)+f2(n%b[i],a2,i-1,p);

    }else if(n/b[i]==a){
        return (n/b[i])*count2(c[i],p);



    }else
    {
        return (n/b[i]-1)*count2(c[i],p)+f2(n%b[i],a2,i-1,p);
    }



}
int f3(int n,int a1,int a2,int d,int p )
{
    if(n/b[d]==0&&a2==0) return 0;
    if(n<10) return n+1-(n>=a2);
    int m=n/b[d];
    if(m==0){
        return count3(n,p);
    }
    else{
    if(m>a2)
    {   if(m>a1){
    return (m-2)*count3(c[d],p)+count3(n%b[d],p)+f3(c[d],a1,a2,d-1,p );

    }
        else if(m==a1){
       return (m-1)*count3(c[d],p)+f3(n%b[d],a1,a2,d-1,p );
        }
        else{
             return (m-1)*count3(c[d],p)+count3(n%b[d],p);
        }


    }else if(m==a2)
    {  if(m>a1){
     return (m-1)*count3(c[d],p)+f3(n%b[d],a1,a2,d-1,p);
    }
        else
        {
        return (m)*count3(c[d],p);
        }





    }else{
        if(m>a1){
            return (m-1)*count3(c[d],p)+f3(n%b[d],a1,a2,d-1,p);
        }
        else if(m==a1){
           return m*count3(c[d],p)+f3(n%b[d],a1,a2,d-1,p);
        }
        else
        return m*count3(c[d],p)+count3(n%b[d],p);

    }

    }


}
long long count3(int n,int p)
{
if(n<p) return 0;
    if(n<100)  return n+1-(n>=p);
    int a=p/10;
    int a2=p%10;
    int i=0;
    while(b[i+1]<n) i++;
     if(n/b[i]>a) {
        return (n/b[i]-1)*count3(c[i],p)+f3(n%b[i],a,a2,i-1,p);

    }else if(n/b[i]==a){
        return (n/b[i])*count3(c[i],p);



    }else
    {
        return (n/b[i]-1)*count3(c[i],p)+f3(n%b[i],a,a2,i-1,p);
    }

}
long long countn(int n,int p)
{

   if(n<0) return 0;
   long long d=0,m=p,res=0;
   while(m!=0) {m=m/10;d++;}

   if(d==1)
   {
//cout<<p<<endl;
     res=count1(n,p);


   }else{
   int a=p/10;
   int b=p%10;

if(a==b) res=count2(n,p);
   else res=count3(n,p);


   }

return res;
}


int main()
{
    int t;
    cin>>t;
    while(t--)
{
    cin>>l>>r>>k;
if(l>r) swap(l,r);
//
cout<<r-l+1-countn(r,k)+countn(l-1,k)<<endl;

}
    return 0;
}
2014-01-24 12:32
a1004573547
Rank: 2
等 级:论坛游民
帖 子:78
专家分:25
注 册:2013-3-11
收藏
得分:0 
稍微修正了一下 不过还是错的。。还没测出来
程序代码:
#include <iostream>
#include <algorithm>
using namespace std;
long long count2(int n,int p);
long long count3(int n,int p);
long long l,r,k;
long long b[]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000,10000000000LL};
long long c[]={0,9,99,999,9999,99999,999999,9999999,99999999,999999999LL,9999999999LL,99999999999LL};

long long count1(int n,int p)
{   if(n<0) return 0;
if(n<10) {

    return 1+n-(n>=p);
}

    int i=0;
     while(b[i+1]<n) i++;
     int a=n/b[i];
     if(a==p){
        return count1(n-b[i]+c[i],p);

     }
     else if(a>p) {

        return (n/b[i]-1)*count1(c[i],p)+count1(n%b[i],p);

     }
     else{
       return (n/b[i])*count1(c[i],p)+count1(n%b[i],p);

     }

}
long long f2(int n,int a2,int d,int p)
{

    if(n/b[d]==0&&a2==0) return 0;
    if(n<10) return n+1-(n>=a2);
    int m=n/b[d];
    if(m==0){
        return count2(n,p);
    }
    else{
    if(m>a2)
    {  return (m-1)*count2(c[d],p)+count2(n%b[d],p);

    }else if(m==a2)
    {  return (m)*count2(c[d],p);

    }else{
        return m*count2(c[d],p)+count2(n%b[d],p);

    }

    }


}
long long count2(int n,int p)
{
    if(n<p) return n+1;
    if(n<100) return n+1-(n>=p);
    int a=p/10;
    int a2=p%10;
     int i=0;
     while(b[i+1]<=n) i++;


    if(n/b[i]>a) {
        return (n/b[i]-1)*count2(c[i],p)+count2(n%b[i],p)+f2(c[i],a2,i-1,p);

    }else if(n/b[i]==a){
        return (n/b[i])*count2(c[i],p)+f2(n%b[i],a2,i-1,p);



    }else
    {
        return (n/b[i])*count2(c[i],p)+count2(n%b[i],p);
    }



}
int f3(int n,int a1,int a2,int d,int p )
{
    if(n/b[d]==0&&a2==0) return 0;
    if(n<10) return n+1-(n>=a2);
    int m=n/b[d];
    if(m==0){
        return count3(n,p);
    }
    else{
    if(m>a2)
    {   if(m>a1){
    return (m-2)*count3(c[d],p)+count3(n%b[d],p)+f3(c[d],a1,a2,d-1,p );

    }
        else if(m==a1){
       return (m-1)*count3(c[d],p)+f3(n%b[d],a1,a2,d-1,p );
        }
        else{
             return (m-1)*count3(c[d],p)+count3(n%b[d],p);
        }


    }else if(m==a2)
    {  if(m>a1){
     return (m-1)*count3(c[d],p)+f3(n%b[d],a1,a2,d-1,p);
    }
        else
        {
        return (m)*count3(c[d],p);
        }





    }else{
        if(m>a1){
            return (m-1)*count3(c[d],p)+f3(n%b[d],a1,a2,d-1,p);
        }
        else if(m==a1){
           return m*count3(c[d],p)+f3(n%b[d],a1,a2,d-1,p);
        }
        else
        return m*count3(c[d],p)+count3(n%b[d],p);

    }

    }


}
long long count3(int n,int p)
{
if(n<p) return n+1;
    if(n<100)  return n+1-(n>=p);
    int a=p/10;
    int a2=p%10;
    int i=0;
    while(b[i+1]<=n) i++;
     if(n/b[i]>a) {
        return (n/b[i]-1)*count3(c[i],p)+count3(n%b[i],p)+f3(c[i],a,a2,i-1,p);

    }else if(n/b[i]==a){
        return (n/b[i])*count3(c[i],p)+f3(n%b[i],a,a2,i-1,p);



    }else
    {
        return (n/b[i])*count3(c[i],p)+count3(n%b[i],p);
    }

}
long long countn(int n,int p)
{

   if(n<0) return 0;
   long long d=0,m=p,res=0;
   while(m!=0) {m=m/10;d++;}

   if(d==1)
   {
//cout<<p<<endl;
     res=count1(n,p);


   }else{
   int a=p/10;
   int b=p%10;

if(a==b) res=count2(n,p);
   else res=count3(n,p);


   }

return res;
}


int main()
{
    int t;
    cin>>t;
    while(t--)
{
    cin>>l>>r>>k;
if(l>r) swap(l,r);
//
cout<<r-l+1-countn(r,k)+countn(l-1,k)<<endl;

}
    return 0;
}
2014-01-24 15:35
a1004573547
Rank: 2
等 级:论坛游民
帖 子:78
专家分:25
注 册:2013-3-11
收藏
得分:0 
再贴一个 ,,,不知道哪里错了  测了100多组数据 和那个前面那个暴力解得一样啊
程序代码:
#include <iostream>
#include <algorithm>
using namespace std;
long long count2(long long n,long long p);
long long count3(long long n,long long p);
long long l,r,k;
long long b[]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000,10000000000LL};
long long c[]={0,9,99,999,9999,99999,999999,9999999,99999999,999999999LL,9999999999LL,99999999999LL};

long long count1(long long n,long long p)
{
if(n<10) {

    return 1+n-(n>=p);
}

     long long i=0;
     while(b[i+1]<=n) i++;
     long long a=n/b[i];
     if(a==p){
        return (n/b[i])*count1(c[i],p);

     }
     else if(a>p) {

        return (n/b[i]-1)*count1(c[i],p)+count1(n%b[i],p);

     }
     else{
       return (n/b[i])*count1(c[i],p)+count1(n%b[i],p);

     }

}
long long f2(long long n,long long a2,long long d,long long p)
{

    if(n/b[d]==0&&a2==0) return 0;
    if(n<10) return n+1-(n>=a2);
    int m=n/b[d];
    if(m==0){
        return count2(n,p);
    }
    else{
    if(m>a2)
    {  return (m-1)*count2(c[d],p)+count2(n%b[d],p);

    }else if(m==a2)
    {  return (m)*count2(c[d],p);

    }else{
        return m*count2(c[d],p)+count2(n%b[d],p);

    }

    }


}
long long count2(long long n,long long p)
{
    if(n<p) return n+1;
    if(n<100) return n+1-(n>=p);
    int a=p/10;
    int a2=p%10;
     int i=0;
     while(b[i+1]<=n) i++;


    if(n/b[i]>a) {
        return (n/b[i]-1)*count2(c[i],p)+count2(n%b[i],p)+f2(c[i],a2,i-1,p);

    }else if(n/b[i]==a){
        return (n/b[i])*count2(c[i],p)+f2(n%b[i],a2,i-1,p);



    }else
    {
        return (n/b[i])*count2(c[i],p)+count2(n%b[i],p);
    }



}
int f3(long long n,long long a1,long long a2,long long d,long long p )
{
    if(n/b[d]==0&&a2==0) return 0;
    if(n<10) return n+1-(n>=a2);
    int m=n/b[d];
    if(m==0){
        return count3(n,p);
    }
    else{
    if(m>a2)
    {   if(m>a1){
    return (m-2)*count3(c[d],p)+count3(n%b[d],p)+f3(c[d],a1,a2,d-1,p );

    }
        else if(m==a1){
       return (m-1)*count3(c[d],p)+f3(n%b[d],a1,a2,d-1,p );
        }
        else{
             return (m-1)*count3(c[d],p)+count3(n%b[d],p);
        }


    }else if(m==a2)
    {  if(m>a1){
     return (m-1)*count3(c[d],p)+f3(c[d],a1,a2,d-1,p);
    }
        else
        {
        return (m)*count3(c[d],p);
        }





    }else{
        if(m>a1){
            return (m-1)*count3(c[d],p)+count3(n%b[d],p)+f3(c[d],a1,a2,d-1,p);
        }
        else if(m==a1){
           return m*count3(c[d],p)+f3(n%b[d],a1,a2,d-1,p);
        }
        else
        return m*count3(c[d],p)+count3(n%b[d],p);

    }

    }


}
long long count3(long long n,long long p)
{
if(n<p) return n+1;
    if(n<100)  return n+1-(n>=p);
    int a=p/10;
    int a2=p%10;
    int i=0;
    while(b[i+1]<=n) i++;
     if(n/b[i]>a) {
        return (n/b[i]-1)*count3(c[i],p)+count3(n%b[i],p)+f3(c[i],a,a2,i-1,p);

    }else if(n/b[i]==a){
        return (n/b[i])*count3(c[i],p)+f3(n%b[i],a,a2,i-1,p);



    }else
    {
        return (n/b[i])*count3(c[i],p)+count3(n%b[i],p);
    }

}
long long countn(long long n,long long p)
{

   if(n<0) return 0;
   long long d=0,m=p,res=0;
   while(m!=0) {m=m/10;d++;}

   if(d==1)
   {
     res=count1(n,p);


   }else{
   int a=p/10;
   int b=p%10;

if(a==b) res=count2(n,p);
   else res=count3(n,p);


   }

return res;
}


int main()
{
    int t;
    cin>>t;
    while(t--)
{
    cin>>l>>r>>k;
if(l>r) swap(l,r);

cout<<r-l+1-countn(r,k)+countn(l-1,k)<<endl;

}
    return 0;
}
2014-01-24 17:10
a1004573547
Rank: 2
等 级:论坛游民
帖 子:78
专家分:25
注 册:2013-3-11
收藏
得分:0 
测了几百万组数据,应该是没错了。。。。。貌似是爆栈
2014-01-24 17:53
a1004573547
Rank: 2
等 级:论坛游民
帖 子:78
专家分:25
注 册:2013-3-11
收藏
得分:0 
我再测测
2014-01-24 18:01
a1004573547
Rank: 2
等 级:论坛游民
帖 子:78
专家分:25
注 册:2013-3-11
收藏
得分:0 
终于ac了  一个小错误。。。。折腾了半天
程序代码:
#include <iostream>
#include <algorithm>
using namespace std;
long long count2(long long n,long long p);
long long count3(long long n,long long p);
long long l,r,k;
long long b[]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000,10000000000LL};
long long c[]={-1,9,99,999,9999,99999,999999,9999999,99999999,999999999LL,9999999999LL,99999999999LL};
long long cc[20][4];
long long count1(long long n,long long p)
{if(n<0) return 0;
if(n<10) {
return 1+n-(n>=p);
}

     long long i=0;
     while(b[i+1]<=n) i++;
     long long a=n/b[i];
     if(a==p){
        return (n/b[i])*count1(c[i],p);

     }
     else if(a>p) {

        return (n/b[i]-1)*count1(c[i],p)+count1(n%b[i],p);

     }
     else{
       return (n/b[i])*count1(c[i],p)+count1(n%b[i],p);

     }

}
long long f2(long long n,long long a2,long long d,long long p)
{
if(n<0) return 0;
    int m=n/b[d];
    if(n/b[d]==0&&a2==0) return 0;
    if(n<10&&m!=0) return n+1-(n>=a2);

    if(m==0){
        return count2(n,p);
    }
    else{
    if(m>a2)
    {  return (m-1)*count2(c[d],p)+count2(n%b[d],p);

    }else if(m==a2)
    {  return (m)*count2(c[d],p);

    }else{
        return m*count2(c[d],p)+count2(n%b[d],p);

    }

    }


}
long long count2(long long n,long long p)
{    if(n<0) return 0;
    if(n<p) return n+1;
    if(n<100) return n+1-(n>=p);
    int a=p/10;
    int a2=p%10;
     int i=0;
     while(b[i+1]<=n) i++;


    if(n/b[i]>a) {
        return (n/b[i]-1)*count2(c[i],p)+count2(n%b[i],p)+f2(c[i],a2,i-1,p);

    }else if(n/b[i]==a){
        return (n/b[i])*count2(c[i],p)+f2(n%b[i],a2,i-1,p);



    }else
    {
        return (n/b[i])*count2(c[i],p)+count2(n%b[i],p);
    }



}
int f3(long long n,long long a1,long long a2,long long d,long long p )
{if(n<0) return 0;

    int m=n/b[d];
    if(n/b[d]==0&&a2==0) return 0;
    if(n<10&&m!=0) return n+1-(n>=a2);


    if(m==0){
        return count3(n,p);
    }
    else{
    if(m>a2)
    {   if(m>a1){
    return (m-2)*count3(c[d],p)+count3(n%b[d],p)+f3(c[d],a1,a2,d-1,p );

    }
        else if(m==a1){
       return (m-1)*count3(c[d],p)+f3(n%b[d],a1,a2,d-1,p );
        }
        else{
             return (m-1)*count3(c[d],p)+count3(n%b[d],p);
        }


    }else if(m==a2)
    {  if(m>a1){
     return (m-1)*count3(c[d],p)+f3(c[d],a1,a2,d-1,p);
    }
        else
        {
        return (m)*count3(c[d],p);
        }





    }else{
        if(m>a1){
            return (m-1)*count3(c[d],p)+count3(n%b[d],p)+f3(c[d],a1,a2,d-1,p);
        }
        else if(m==a1){
           return m*count3(c[d],p)+f3(n%b[d],a1,a2,d-1,p);
        }
        else
        return m*count3(c[d],p)+count3(n%b[d],p);

    }

    }


}
long long count3(long long n,long long p)
{if(n<0) return 0;

if(n<p) return n+1;
    if(n<100)  return n+1-(n>=p);
    int a=p/10;
    int a2=p%10;
    int i=0;
    while(b[i+1]<=n) i++;
     if(n/b[i]>a) {
        return (n/b[i]-1)*count3(c[i],p)+count3(n%b[i],p)+f3(c[i],a,a2,i-1,p);

    }else if(n/b[i]==a){
        return (n/b[i])*count3(c[i],p)+f3(n%b[i],a,a2,i-1,p);



    }else
    {
        return (n/b[i])*count3(c[i],p)+count3(n%b[i],p);
    }

}
long long countn(long long n,long long p)
{

   if(n<0) return 0;
   long long d=0,m=p,res=0;
   while(m!=0) {m=m/10;d++;}

   if(d==1)
   {
     res=count1(n,p);


   }else{
   int a=p/10;
   int b=p%10;

if(a==b) res=count2(n,p);
   else res=count3(n,p);


   }

return res;
}


int main()
{
    int t;
    cin>>t;
    while(t--)
{
    cin>>l>>r>>k;
if(l>r) swap(l,r);

cout<<r-l+1-countn(r,k)+countn(l-1,k)<<endl;

}
    return 0;
}
2014-01-24 20:08
beyondyf
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:103
帖 子:3282
专家分:12654
注 册:2008-1-21
收藏
得分:0 
那个子矩阵的问题目前还没什么好的想法。可以确定的是,靠一个一个数是肯定不行的,即使能排除部分不成倍数的子矩阵。应该还是要应用一些目前我没掌握的数论方面的结论。

先把这题我的代码发上来。虽然楼主给了我他的帐号,但没找到这题的地址呵呵,还是想直接提交看看结果比较好。

自己生成了三十万组随机示例,并写了一个可靠的穷举代码以对比结果。应该没什么问题了。那几个很长的逻辑表达式的调试费了我很长时间,非常容易弄错。

程序代码:
#include<stdio.h>

int count(int a[][10], int p0, int p1, int n)
{
    int b[8], bn, c = 0, i, m;
    if(n <= 0) return 0;
    for(m = n, bn = 0; m; m /= 10) b[bn++] = m % 10;
    for(m = --bn; m >= 0; m--)
    {
        for(i = 0; i < b[m]; i++)
            if(i != p0 || p1 && (m == bn || b[m + 1] != p1)) c += a[m][i];
        if(b[m] == p0 && (!p1 || m < bn && b[m + 1] == p1)) break;
    }
    if(
        m < 0 &&
        (bn || p1 || b[0] != p0) &&
        (!bn || b[0] != p0 || p1 && b[1] != p1)
    ) c++; 
    return n - c + 1;
}

int cal(int L, int R, int p)
{
    int a[8][10], p0, p1, c, i, j;

    p0 = p % 10;
    p1 = p / 10;
    for(c = j = 0; j <= 9; j++) c += a[0][j] = p != j;

    for(i = 1; i < 8; i++)
    for(a[i][0] = c, c = 0, j = 0; j <= 9; c += a[i][j++])
        a[i][j] = (j==p) ? 0 : (j==p1) ? a[i][0] - a[i-1][p0] : a[i][0];

    return count(a, p0, p1, R) - count(a, p0, p1, L - 1);
}

int main()
{
    int T, L, R, p;
    for(scanf("%d", &T); T--; printf("%d\n", cal(L, R, p)))
        scanf("%d%d%d", &L, &R, &p);
    return 0;
}

重剑无锋,大巧不工
2014-01-26 18:08
a1004573547
Rank: 2
等 级:论坛游民
帖 子:78
专家分:25
注 册:2013-3-11
收藏
得分:0 
错误了
2014-01-27 09:20
a1004573547
Rank: 2
等 级:论坛游民
帖 子:78
专家分:25
注 册:2013-3-11
收藏
得分:0 
算法思想是什么? 太多符号很难读
2014-01-27 09:33
快速回复:一个数位dp的题目
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.064684 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved