[学习笔记] 类欧几里得算法
考虑递归进行处理
要不断凑出结构
为了防止n过大,对a>=c||b>=c与否进行讨论处理
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<bits/stdc++.h> #define reg register int #define il inline #define fi first #define se second #define mk(a,b) make_pair(a,b) #define numb (ch^'0') using namespace std; typedef long long ll; template<class T>il void rd(T &x){ char ch;x=0;bool fl=false; while(!isdigit(ch=getchar()))(ch=='-')&&(fl=true); for(x=numb;isdigit(ch=getchar());x=x*10+numb); (fl==true)&&(x=-x); } template<class T>il void output(T x){if(x/10)output(x/10);putchar(x%10+'0');} template<class T>il void ot(T x){if(x<0) putchar('-'),x=-x;output(x);putchar(' ');} template<class T>il void prt(T a[],int st,int nd){for(reg i=st;i<=nd;++i) ot(a[i]);putchar('\n');} namespace Miracle{ const int mod=998244353; const int iv2=499122177; int t; ll f(ll a,ll b,ll c,ll n){ if(a==0){ return (n+1)*(b/c)%mod; } if(a>=c||b>=c){ return (f(a%c,b%c,c,n)+(a/c)*n%mod*(n+1)%mod*iv2%mod+(n+1)*(b/c)%mod)%mod; } ll m=(a*n+b)/c; return (n*m%mod-f(c,c-b-1,a,m-1)+mod)%mod; } int main(){ int t; rd(t); ll n,a,b,c; while(t--){ rd(n);rd(a);rd(b);rd(c); printf("%lld 233 233\n",f(a,b,c,n)); } return 0; } } signed main(){ Miracle::main(); return 0; } /* Author: *Miracle* Date: 2019/4/13 19:58:12 */
咕