CodeForces 107D

本文介绍了一种使用矩阵快速幂算法解决特定动态规划问题的方法。通过定义状态转移矩阵并利用快速幂进行高效计算,解决了给定条件下求解动态规划的问题。

矩阵快速幂
写出dp方程
状态就是所有模数的余数值的所有情况

#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
typedef __int64 LL;
const int mod=12345;
const int N=130;
vector<int> ve[30];
#define pb push_back
int top,ww[N],sta[N];
int ll[30],rr[30];

void cp(int A[N][N],int B[N][N],int n){
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            A[i][j]=B[i][j];
        }
    }
}

int D[N][N];
void _mul(int A[N][N],int B[N][N],int n){
    memset(D,0,sizeof(D));
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            for(int k=0;k<n;k++){
                D[i][j]+=A[i][k]*B[k][j]%mod;
                D[i][j]%=mod;
            }
        }
    }
    cp(A,D,n);
}

int C[N][N];
void _pow(int A[N][N],LL b,int n){
    memset(C,0,sizeof(C));
    for(int i=0;i<n;i++){
        C[i][i]=1;
    }
    while(b){
        if(b&1){
            _mul(C,A,n);
        }
        _mul(A,A,n);
        b/=2;
    }
    cp(A,C,n);
}

void fco(int aa[N],int i){
    for(int j=top-1;j>=0;j--){
        aa[j]=i/ww[j];
        i%=ww[j];
    }
}

int A[N][N],B[N][N];
int aa[N];
void getA(int co){
    memset(A,0,sizeof(A));
    for(int i=0;i<co;i++){
        for(int j=0;j<26;j++)if(ve[j].size()){
            if(ve[j][0]==1){
                A[i][i]++;
            }
            else {
                fco(aa,i);

                for(int jj=ll[j];jj<=rr[j];jj++){
                    aa[jj]++;aa[jj]%=sta[jj];
                }

                int ii=0;
                for(int jj=0;jj<top;jj++){
                    ii+=aa[jj]*ww[jj];
                }
                A[ii][i]++;
            }
        }
    }
}


void getB(){
    memset(B,0,sizeof(B));
    for(int i=0;i<26;i++)if(ve[i].size()){
        if(ve[i][0]==1){
            B[0][0]++;
        }
        else {
            int ans=0;
            for(int j=ll[i];j<=rr[i];j++){
                ans+=ww[j];
            }
            B[ans][0]++;
        }
    }
}

int geta(int co){
    int ans=0;
    for(int i=0;i<co;i++){
        fco(aa,i);
        int fg=1;
        for(int j=0;j<26;j++)if(ve[j].size()&&ve[j][0]!=1){
            int flag=0;
            for(int jj=ll[j];jj<=rr[j];jj++){
                if(aa[jj]==0){
                    flag=1;break;
                }
            }
            fg=flag;
            if(!fg)break;
        }
        if(fg){
            ans+=A[i][0];ans%=mod;
        }
    }
    return ans;
}

int main(){
    #ifdef DouBi
    freopen("in.cpp","r",stdin);
    #endif // DouBi
    LL n;int c;
    while(scanf("%I64d%d",&n,&c)!=EOF){
        for(int i=0;i<26;i++){
            ve[i].clear();
        }
        for(int i=0;i<c;i++){
            char ss[2];int a;
            scanf("%s%d",ss,&a);
            ve[ss[0]-'A'].pb(a);
        }
        if(n==0){
            printf("1\n");continue;
        }

        for(int i=0;i<26;i++){
            sort(ve[i].begin(),ve[i].end());
            ve[i].erase(unique(ve[i].begin(),ve[i].end()),ve[i].end());
            //printf("%d\n",ve[i].size());
        }

        int co=1;
        top=0;
        for(int i=0;i<26;i++)if(ve[i].size()&&ve[i][0]!=1){
            ll[i]=top;
            for(int j=0;j<ve[i].size();j++){
                sta[top]=ve[i][j];
                ww[top]=co;
                co*=ve[i][j];
                top++;
            }
            rr[i]=top-1;
        }

        getA(co);
        _pow(A,n-1,co);
        getB();
        _mul(A,B,co);
        printf("%d\n",geta(co));
    }
    return 0;
}
### Codeforces 1487D Problem Solution The problem described involves determining the maximum amount of a product that can be created from given quantities of ingredients under an idealized production process. For this specific case on Codeforces with problem number 1487D, while direct details about this exact question are not provided here, similar problems often involve resource allocation or limiting reagent type calculations. For instance, when faced with such constraints-based questions where multiple resources contribute to producing one unit of output but at different ratios, finding the bottleneck becomes crucial. In another context related to crafting items using various materials, it was determined that the formula `min(a[0],a[1],a[2]/2,a[3]/7,a[4]/4)` could represent how these limits interact[^1]. However, applying this directly without knowing specifics like what each array element represents in relation to the actual requirements for creating "philosophical stones" as mentioned would require adjustments based upon the precise conditions outlined within 1487D itself. To solve or discuss solutions effectively regarding Codeforces' challenge numbered 1487D: - Carefully read through all aspects presented by the contest organizers. - Identify which ingredient or component acts as the primary constraint towards achieving full capacity utilization. - Implement logic reflecting those relationships accurately; typically involving loops, conditionals, and possibly dynamic programming depending on complexity level required beyond simple minimum value determination across adjusted inputs. ```cpp #include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); for(int i=0;i<n;++i){ cin>>a[i]; } // Assuming indices correspond appropriately per problem statement's ratio requirement cout << min({a[0], a[1], a[2]/2LL, a[3]/7LL, a[4]/4LL}) << endl; } ``` --related questions-- 1. How does identifying bottlenecks help optimize algorithms solving constrained optimization problems? 2. What strategies should contestants adopt when translating mathematical formulas into code during competitive coding events? 3. Can you explain why understanding input-output relations is critical before implementing any algorithmic approach? 4. In what ways do prefix-suffix-middle frameworks enhance model training efficiency outside of just tokenization improvements? 5. Why might adjusting sample proportions specifically benefit models designed for tasks requiring both strong linguistic comprehension alongside logical reasoning skills?
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值