dp practice 1

https://codeforces.com/problemset/problem/553/A

dp+组合数学

dp[i] 放前i种颜色的方法数

dp practice 1
 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e3+7;
const ll mod = 1e9+7;
int a[N];
ll dp[N],f[N];
void work(){
    f[0]=1;
    f[1]=1;
    for(int i=2;i<N;i++)
        f[i]=f[i-1]*i%mod;
}
ll q_pow(ll a,ll n){
    ll ans=1; ll base=a;
    while(n){
        if(n&1) ans=(ans*base)%mod;
        base=base*base%mod;
        n>>=1;
    }
        return ans;
}
ll inv(ll a,ll b){
    return q_pow(a,b-2);
} 
ll C(int n,int m){
    return f[n]*inv(f[n-m]*f[m]%mod,mod)%mod;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int k; cin>>k;
    for(int i=1;i<=k;i++){
        cin>>a[i];    
    }
    work();
    int sum=a[1];
    dp[1]=1;
    for(int i=2;i<=k;i++){
        dp[i]=(dp[i-1]*C(sum+a[i]-1,a[i]-1))%mod;
        sum+=a[i];
    }
    cout<<dp[k]<<endl;
    return 0;
}
View Code

https://codeforces.com/problemset/problem/264/B

dp+简单数论

dp[i] 表示当前当前位置因子为i的所能构成的序列长度

dp practice 1
 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+7;
const ll mod = 1e9+7;
int a[N];
int dp[N];
vector<int> v[N];
void work(){
    for(int i=2;i<N;i++)
        for(int j=i;j<N;j+=i)
            v[j].push_back(i);
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int n; cin>>n;
    work();
    for(int i=1;i<=n;i++) cin>>a[i];
    v[1].push_back(1);
    for(int i=1;i<=n;i++){
        int tmp=0;
        for(int j=0;j<v[a[i]].size();j++)
            tmp=max(tmp,dp[v[a[i]][j]]);
        for(int j=0;j<v[a[i]].size();j++)
            dp[v[a[i]][j]]=tmp+1;
    }
    int ans=0;
    for(int i=1;i<N;i++) ans=max(ans,dp[i]);
    cout<<ans<<endl; 
    return 0;
}
View Code

 

上一篇:advanced mathematics 3


下一篇:How to sort HashSet in Java