B1072 [SCOI2007]排列perm 狀壓dp

來源:互聯網
上載者:User

標籤:std   scan   queue   +=   name   typedef   des   包含   write   

很簡單的狀壓dp,但是有一個事,就是。。。我數組開大了一點,然後每次memset就會T,然後開小就好了!!!震驚!以後小心點這個問題。

題幹:

Description  給一個數字串s和正整數d, 統計s有多少種不同的排列能被d整除(可以有前置0)。例如123434有90種排列能被2整除,其中末位為2的有30種,末位為4的有60種。Input  輸入第一行是一個整數T,表示測試資料的個數,以下每行一組s和d,中間用空格隔開。s保證只包含數字0, 1, 2, 3, 4, 5, 6, 7, 8, 9.Output  每個資料僅一行,表示能被d整除的排列的個數。Sample Input7000 1001 11234567890 1123434 21234 712345 1712345678 29Sample Output13362880090361398HINT在前三個例子中,排列分別有1, 3, 3628800種,它們都是1的倍數。【限制】100%的資料滿足:s的長度不超過10, 1<=d<=1000, 1<=T<=15Source

代碼:

#include<iostream>#include<cstdio>#include<cmath>#include<ctime>#include<queue>#include<algorithm>#include<cstring>using namespace std;#define duke(i,a,n) for(int i = a;i <= n;i++)#define lv(i,a,n) for(int i = a;i >= n;i--)#define clean(a) memset(a,0,sizeof(a))const int INF = 1 << 30;typedef long long ll;typedef double db;template <class T>void read(T &x){    char c;    bool op = 0;    while(c = getchar(), c < ‘0‘ || c > ‘9‘)        if(c == ‘-‘) op = 1;    x = c - ‘0‘;    while(c = getchar(), c >= ‘0‘ && c <= ‘9‘)        x = x * 10 + c - ‘0‘;    if(op) x = -x;}template <class T>void write(T x){    if(x < 0) putchar(‘-‘), x = -x;    if(x >= 10) write(x / 10);    putchar(‘0‘ + x % 10);}char s[20];int t;ll d;ll dp[1 << 12][1003];ll num[20],p[20];int main(){    read(t);    while(t--)    {        clean(dp);clean(num);clean(p);        scanf("%s",s + 1);        int l = strlen(s + 1);//        cout<<l<<endl;        duke(i,1,l)        num[s[i] - ‘0‘]++,p[i] = s[i] - ‘0‘;        read(d);        dp[0][0] = 1;                duke(i,0,(1 << l) - 1)        {            duke(j,0,d - 1)            {                duke(k,1,l)                {                    if((i & (1 << (k - 1))) == 0)                    {                        dp[i | (1 << (k - 1))][(j * 10 + p[k]) % d] += dp[i][j];                    }                }            }        }//        cout<<"QAQ"<<endl;        ll ans = dp[(1 << l) - 1][0];         duke(i,0,9)        {            duke(j,1,num[i])            ans /= j;        }        printf("%lld\n",ans);     }    return 0; } /*7000 1001 11234567890 1123434 21234 712345 1712345678 29*/

 

B1072 [SCOI2007]排列perm 狀壓dp

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.