Codeforces 919E 小学数学入门

题意:给你a、b、p(素数,<=1e6+3)、x(<=1e12),

满足n (1 ≤ n ≤ x) 且

的n个数。


题解:等式左边分开考虑,n%p显然最小循环周期是p,而a^n%p最小循环周期是p-1,因为n=0时,a^n%p=1,由费马小定理,a^(p-1)%p=1,得证。

还有一个事情就是n*a^n%p的最小循环周期是p*(p-1),这个事情大概可以这样考虑:显然LCM(p,(p-1))=p*(p-1)是一个循环周期,然后也找不到更小的了,得证…………感觉有点草率OTZ。

 对于每一个n,因为a^n的最小循环周期是p-1,因此我们可以在1..p-1范围枚举n,对于每一个a^n独立求解,那么我们先计算出a^n,用逆元除到右边去,使得这个等式变成 n和b/a^n同余,因为我们固定了a^n,所以我们的n每次只能加上p-1,这样给n带来的影响就是变成了(n+p-1)%p=(n-1)%p,那么我们可以很轻松的得到最小的满足条件的正整数n,再结合x的范围,以及周期=p*(p-1)可以得到这一个a^n对答案的总贡献……就做完了。。。

脑子是个好东西……可惜我没有……

Code:

include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL a,b,p,x;
LL quick(LL x,LL y,LL p){
    LL res=1;
    while (y){
        if (y&1)res = res*x%p;
        x = x*x%p;
        y/=2;
    }
    return res;
}
int main(){
    cin>>a>>b>>p>>x;
    LL ans =0;
    LL temp = a;
    for (LL i=1;i<p;i++,temp=temp*a%p){
        LL c = b*quick(temp,p-2,p)%p;
        LL k = (i-c+p)%p;
        LL minn = i+k*(p-1);
        if (minn>x)continue;
        ans += max(0LL,(x-minn)/(p*(p-1))+1);
    }
    cout<<ans<<endl;
    return 0;
}



### Codeforces 887E Problem Solution and Discussion The problem **887E - The Great Game** on Codeforces involves a strategic game between two players who take turns to perform operations under specific rules. To tackle this challenge effectively, understanding both dynamic programming (DP) techniques and bitwise manipulation is crucial. #### Dynamic Programming Approach One effective method to approach this problem utilizes DP with memoization. By defining `dp[i][j]` as the optimal result when starting from state `(i,j)` where `i` represents current position and `j` indicates some status flag related to previous moves: ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = ...; // Define based on constraints int dp[MAXN][2]; // Function to calculate minimum steps using top-down DP int minSteps(int pos, bool prevMoveType) { if (pos >= N) return 0; if (dp[pos][prevMoveType] != -1) return dp[pos][prevMoveType]; int res = INT_MAX; // Try all possible next positions and update 'res' for (...) { /* Logic here */ } dp[pos][prevMoveType] = res; return res; } ``` This code snippet outlines how one might structure a solution involving recursive calls combined with caching results through an array named `dp`. #### Bitwise Operations Insight Another critical aspect lies within efficiently handling large integers via bitwise operators instead of arithmetic ones whenever applicable. This optimization can significantly reduce computation time especially given tight limits often found in competitive coding challenges like those hosted by platforms such as Codeforces[^1]. For detailed discussions about similar problems or more insights into solving strategies specifically tailored towards contest preparation, visiting forums dedicated to algorithmic contests would be beneficial. Websites associated directly with Codeforces offer rich resources including editorials written after each round which provide comprehensive explanations alongside alternative approaches taken by successful contestants during live events. --related questions-- 1. What are common pitfalls encountered while implementing dynamic programming solutions? 2. How does bit manipulation improve performance in algorithms dealing with integer values? 3. Can you recommend any online communities focused on discussing competitive programming tactics? 4. Are there particular patterns that frequently appear across different levels of difficulty within Codeforces contests?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值