codeforces [Gym-100814K]

本文介绍了一种利用动态规划(DP)解决大数除法后特定子串组合计数的问题。通过模拟除法过程获取小数点后n位,并采用DP策略计算这些位数中能被特定数整除的子串数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意给你一个a,b,问a除b小数点后n位中,子串组合的数能被p整除的有多少个
a,b位1e16,n为1e6,p为300
思路首先,要先处理出来后小数点后n位,这个就是模拟一下除法,很简单(当时没想到哈)这是一个有意思的“迪屁”这个梗是去年,北大吉如一比赛之后和队友说:这比赛太垃圾了,满场都是数据结构,连一个有意思的迪屁都没有。
接下来就该转移了,转移方程为dp[i][x] = g[i - 1][y] + int(a[i] % p == x)这个表示,到i这一位,子串能构成x的倍数有多少个,那个y满足,(y + a[i]) % p=x,y=(x - a[i] + p) % p;当你转移完了这一位的时候,当前i位对i+1位的影响就是x * 10,所以转移的时候不要忘了乘10,来作为下一位的转移的根据。
每一位都是由上一位转移过来,所以就可以滚动一下,滚第一维
PS:一个有点玄学的地方就是,如果你把n和p开成long long 就wrong了,我至今也不知道怎么回事,及其玄学。

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <cstring>
#include <map>

using namespace std;
typedef long long LL;

const int N = 1e6;
LL A[N + 5] , dp[2][300] , g[2][300];
LL a , b ; int  p ,n ;

int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%lld %lld %d %d",&a , &b , &n , &p);
        for(int i = 1 ; i <= n ; i++){
            A[i] = (a * 10 / b);
            a = a * 10 % b;
        }
        memset(dp , 0 , sizeof(dp));
        memset(g , 0 , sizeof(g));
        LL  ans = 0;
        int t = 0;
        for(int i = 1 ; i <= n ; i++){
            t = 1 - t;
            memset(g[t] , 0 , sizeof(g[t]));
            for(int x = 0 ; x < p ; x++){
                int y = (x - A[i] + p) % p;
                dp[t][x] = g[1 - t][y] + (LL)((A[i]) % p == x);
                g[t][(x * 10) % p] += dp[t][x];
            }
            ans = ans + dp[t][0];
        }
        printf("%lld\n",ans);
    }
    return 0;
}
### Codeforces Problem 976C Solution in Python For solving problem 976C on Codeforces using Python, efficiency becomes a critical factor due to strict time limits aimed at distinguishing between efficient and less efficient solutions[^1]. Given these constraints, it is advisable to focus on optimizing algorithms and choosing appropriate data structures. The provided code snippet offers insight into handling string manipulation problems efficiently by customizing comparison logic for sorting elements based on specific criteria[^2]. However, for addressing problem 976C specifically, which involves determining the winner ('A' or 'B') based on frequency counts within given inputs, one can adapt similar principles of optimization but tailored towards counting occurrences directly as shown below: ```python from collections import Counter def determine_winner(): for _ in range(int(input())): count_map = Counter(input().strip()) result = "A" if count_map['A'] > count_map['B'] else "B" print(result) determine_winner() ``` This approach leverages `Counter` from Python’s built-in `collections` module to quickly tally up instances of 'A' versus 'B'. By iterating over multiple test cases through a loop defined by user input, this method ensures that comparisons are made accurately while maintaining performance standards required under tight computational resources[^3]. To further enhance execution speed when working with Python, consider submitting codes via platforms like PyPy instead of traditional interpreters whenever possible since they offer better runtime efficiencies especially important during competitive programming contests where milliseconds matter significantly.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值