"Noi 2.6_9289" & "bzoj2023/1630" ant counting number ant {Usaco2005 Nov}

Source: Internet
Author: User

Test instructions: An ant with a family of M, each NI only (the same as each other). Ask how many different combinations of l~r are selected.

Solution: A very basic DP, do not be "permutation combination" confused ah ~ I have been exposed to this type, but also forgot, must remember!
This is a type of DP--M species N for DP, which defines F[I][J] to denote the number of schemes of J ("Only" ants) selected in the first I species (this is "family"). Then the layered DP.

So F[i][j]=sum{f[i-1][j-k]} (0<=K<=NI), then use prefix and optimize time + scroll array to optimize the space.

1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <iostream>5#include <algorithm>6 using namespacestd;7 #defineM 10108 #defineN 1000109 #defineMoD 1000000Ten  One inta[n],h[m],f[2][n];//H[i] is the problem of NI A intMmin (intXintY) {returnX<y?x:y;} -  - intMain () the { -     intM,n,l,r; -scanf"%d%d%d%d",&m,&n,&l,&R); -      for(intI=1; i<=n;i++) scanf ("%d",&a[i]); +Sort (A +1, A +1+n); -     intt=1; h[1]=1, h[m+1]=n+1; +      for(intI=2; i<=n;i++) A       if(a[i]!=a[i-1]) h[++t]=i; at    -f[0][0]=1; -      for(intj=1; j2];j++) f[0][j]=0; -     intu=1; -      for(intI=1; i<=m;i++) -     { inf[u][0]=1; -        for(intj=1; j1];j++) to       { +         intK=mmin (h[i+1]-h[i],j); -F[u][j]= ((f[u][j-1]+f[1-U][J])%mod-f[1-u][j-k-1]%MOD+MOD)%MoD; the         //for (int k=0;k<=h[i+1]-h[i]&&k<=j;k++) *         //F[i][j]+=f[i-1][j-k]; $       }Panax Notoginsengu=1-u; -     } the     intans=0; +      for(intj=l;j<=r;j++) ans= (ans+f[1-U][J])%MoD; Aprintf"%d\n", ans); the     return 0; +}

"Noi 2.6_9289" & "bzoj2023/1630" ant counting number ant {Usaco2005 Nov}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.