Monday, 18 April 2016

* Writing Code

 Writing Code


Programmers working on a large project have just received a task to write exactly m lines of code. There are n programmers working on a project, the i-th of them makes exactly ai bugs in every line of code that he writes.
Let's call a sequence of non-negative integers v1, v2, ..., vn a plan, if v1 + v2 + ... + vn = m. The programmers follow the plan like that: in the beginning the first programmer writes the first v1 lines of the given task, then the second programmer writes v2 more lines of the given task, and so on. In the end, the last programmer writes the remaining lines of the code. Let's call a plan good, if all the written lines of the task contain at most b bugs in total.
Your task is to determine how many distinct good plans are there. As the number of plans can be large, print the remainder of this number modulo given positive integer mod.
Input
The first line contains four integers nmbmod (1 ≤ n, m ≤ 5000 ≤ b ≤ 5001 ≤ mod ≤ 109 + 7) — the number of programmers, the number of lines of code in the task, the maximum total number of bugs respectively and the modulo you should use when printing the answer.
The next line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 500) — the number of bugs per line for each programmer.
Output
Print a single integer — the answer to the problem modulo mod.
Sample test(s)
input
3 3 3 100
1 1 1
output
10
input
3 6 5 1000000007
1 2 3
output
0
input
3 5 6 11
1 2 1
output
0

----------------------------------------------------editoril----------------------------------------------------------------

  recursive dp  solution will give memory limit ex..
recursive sol
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef long int li;
typedef unsigned long long int ulli;
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define test() int t; cin>>t;while(t--)

#define si(x) scanf("%d",&x)
#define slli(x) scanf("%lld",&x)
#define sli(x) scanf("%ld",&x)
#define sd(x) scanf("%d",&x)

#define pfi(x) printf("%d\n",x)
#define pfli(x) printf("%ld\n",x)
#define pflli(x) printf("%lld\n",x)

#define abs(x) ((x)>0?(x):-(x))


#define TRI(a,b,c) mp(mp(a,b),c)

#define INDEX(arr,ind) (lower_bound(all(arr),ind)-arr.begin())
#define all(x) x.begin(),x.end()
#define sz size()

#define rep(i,a,b) for(int  i=(a);i<(b);i++)
#define repl(i,a,b) for(lli  i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b);i>=(a);i--)
#define foreach( gg,itit ) for( typeof(gg.begin()) itit=gg.begin();itit!=gg.end();itit++ )
//struct Sync_stdio { Sync_stdio() { cin.tie(NULL); ios_base::sync_with_stdio(false); } } _sync_stdio;
typedef pair<lli,lli> PII;
typedef vector<int> VI;
typedef vector<PII> VPII;

int dx[]={1,-1,0,0};
int dy[]={0,0,-1,1};
lli  comb[1001][1001];


 
 lli gcd(lli a, lli b){ if(b)return gcd(b,a%b); return a;}
 
 inline void add(int &x, int y){x+=y; if(x>=mod)x-=mod; if(x<0)x+=mod;} // (a+b)%mod;
 
lli mulmod ( lli a , lli b,lli mod_val)  // (a*b)%c for large values of a and b 
{
 lli ret = 0;
 while(b)
 {
  if(b&1)
  {
   ret += a;
   if(ret >= mod_val)
    ret %= mod_val;
  }
  a = a + a;
  if(a >= mod_val) a %= mod_val;
  b >>= 1;
 }
 return ret;
}


 long long int read_int(){
 char r;
 bool start=false,neg=false;
 long long int ret=0;
 while(true){
  r=getchar();
  if((r-'0'<0 || r-'0'>9) && r!='-' && !start){
   continue;
  }
  if((r-'0'<0 || r-'0'>9) && r!='-' && start){
   break;
  }
  if(start)ret*=10;
  start=true;
  if(r=='-')neg=true;
  else ret+=r-'0';
 }
 if(!neg)
  return ret;
 else
  return -ret;
}
 
 lli combination()
  {
  
   for(int i = 0; i < 1000; i++) {
        comb[i][0] = 1;
        for(int j = 1; j <= i; j++)
            comb[i][j] = (comb[i - 1][j] + comb[i - 1][j - 1]) % mod;
    }
  
  }
 
 
 /*
void dfs()
{
 
}

void bfs()
{
  
}

void lps()
{
   
}
 */
 /*----------------------------------------------------------------------------*/
int arr[1000];
int dp[501][501][501];
int n,m,b;
lli solve(int index,int line,int error,lli mo)
{
// cout<<index<<" "<<line<<" "<<error<<" "<<mo<<endl;
   if(index>=n || error>b || line>m) return 0;
   else if(line==m && error<=b) 
   {
    //cout<<"base cse "<<endl;
    return 1;
   }
   else if(line==m && error >b) return 0;
   else if(dp[index][line][error]!=-1) return dp[index][line][error];
   else
   {
      lli ans=0;
      ans=solve(index,line+1,error+arr[index],mo);
      ans=(ans+solve(index+1,line,error,mo))%mo;
      dp[index][line][error]=ans;
      return ans;
   }
   
}

int main()
{
// freopen("abc.txt","r",stdin);
 //freopen("pqr.txt","w",stdout);
 memset(dp,-1,sizeof dp);
    
    lli mo;
    
    si(n);
    si(m);
    si(b);
    slli(mo);
    
    for(int i=0;i<n;i++) cin>>arr[i];
   lli ans= solve(0,0,0,mo);
   pflli(ans);
  }
---------------------------------------------iterative soln---------------------------------------------------------------
#include <bits/stdc++.h>
using namespace std;

int n, m , b, B, a[505], d[2][505][505];

int main(){
    scanf("%d%d%d%d",&n,&m,&b,&B);
    for(int i = 0; i < n; i++)
        scanf("%d", a+i);
    for(int i = 0; i < n; i++)
        for(int j = 0; j <= b; j++)
            for(int k = 0; k <= m; k++)
                d[i&1][j][k] = ((i < 1) ? (a[0]*k<=j): (d[1-(i&1)][j][k] + ((j >= a[i] && k > 0) ? d[i&1][j-a[i]][k-1] : 0))) % B;
    printf("%d", d[1-(n&1)][b][m]%B);
    return 0;
}

No comments:

Post a Comment