zoukankan      html  css  js  c++  java
  • [模板] 类欧几里得算法

    [ m f(a,b,c,n)=sumlimits_{i=0}^n lfloor dfrac{ai+b}{c} floor\ g(a,b,c,n)=sumlimits_{i=0}^n ilfloor dfrac{ai+b}{c} floor\ h(a,b,c,n)={sumlimits_{i=0}^n lfloor dfrac{ai+b}{c} floor }^2 ]

    抄了一份板子

    #include <bits/stdc++.h>
    
    #define int long long
    using namespace std;
    const int P = 998244353;
    int i2 = 499122177, i6 = 166374059;
    struct data {
    	data() {
    		f = g = h = 0;
    	}
    	int f, g, h;
    
    
    };  // 三个函数打包
    data calc(int n, int a, int b, int c) {
    	int ac = a / c, bc = b / c, m = (a * n + b) / c, n1 = n + 1,
    	    n21 = n * 2 + 1;
    	data d;
    	if (a == 0) {  // 迭代到最底层
    		d.f = bc * n1 % P;
    		d.g = bc * n % P * n1 % P * i2 % P;
    		d.h = bc * bc % P * n1 % P;
    		return d;
    	}
    	if (a >= c || b >= c) {  // 取模
    		d.f = n * n1 % P * i2 % P * ac % P + bc * n1 % P;
    		d.g = ac * n % P * n1 % P * n21 % P * i6 % P +
    		      bc * n % P * n1 % P * i2 % P;
    		d.h = ac * ac % P * n % P * n1 % P * n21 % P * i6 % P +
    		      bc * bc % P * n1 % P + ac * bc % P * n % P * n1 % P;
    		d.f %= P, d.g %= P, d.h %= P;
    
    		data e = calc(n, a % c, b % c, c);  // 迭代
    
    		d.h += e.h + 2 * bc % P * e.f % P + 2 * ac % P * e.g % P;
    		d.g += e.g, d.f += e.f;
    		d.f %= P, d.g %= P, d.h %= P;
    		return d;
    	}
    	data e = calc(m - 1, c, c - b - 1, a);
    	d.f = n * m % P - e.f, d.f = (d.f % P + P) % P;
    	d.g = m * n % P * n1 % P - e.h - e.f, d.g = (d.g * i2 % P + P) % P;
    	d.h = n * m % P * (m + 1) % P - 2 * e.g - 2 * e.f - d.f;
    	d.h = (d.h % P + P) % P;
    	return d;
    }
    int T, n, a, b, c;
    
    signed main() {
    	scanf("%lld", &T);
    	while (T--) {
    		scanf("%lld%lld%lld%lld", &n, &a, &b, &c);
    		data ans = calc(n, a, b, c);
    		printf("%lld %lld %lld
    ", ans.f, ans.h, ans.g);
    	}
    	return 0;
    }
    

    本文来自博客园,作者:GhostCai,转载请注明原文链接:https://www.cnblogs.com/ghostcai/p/15102319.html

  • 相关阅读:
    Leetcode 814. 二叉树剪枝
    Leetcode 104. 二叉树的最大深度
    Leetcode 617. 合并二叉树
    Leetcode 226. 翻转二叉树
    Leetcode 654.最大二叉树
    【Leetcode】413. Arithmetic Slices
    【Leetcode】128. Longest Consecutive Sequence
    【Leetcode】605. Can Place Flowers
    【Leetcode】647. Palindromic Substrings
    高可用架构
  • 原文地址:https://www.cnblogs.com/ghostcai/p/15102319.html
Copyright © 2011-2022 走看看