zoukankan      html  css  js  c++  java
  • 类欧几里得算法

    (sum_{x=0}^{n-1}lfloor frac{ax+b}{c} floor)

    (f(a,b,c,n)=sumlimits_{x=0}^{n-1}lfloor frac{ax+b}{c} floor)

    进行化简,得

    [egin{aligned} &f(a,b,c,n) \ =&sum_{x=0}^{n-1}lfloor frac{ax+b}{c} floor \ =&sum_{x=0}^{n-1}lfloor frac{(a mod c)x + (a - a mod c)x+(b mod c)+(b - b mod c)}{c} floor \ =&sum_{x=0}^{n-1}lfloor frac{(a mod c)x + (b mod c)}{c} floor + frac{(a - a mod c)x}{c} + frac{b - b mod c}{c}\ =&sum_{x=0}^{n-1}lfloor frac{(a mod c)x + (b mod c)}{c} floor + lfloor frac{a}{c} floor x + lfloor frac{b}{c} floor\ =&frac{n(n-1)}{2}lfloor frac{a}{c} floor + n lfloor frac{b}{c} floor + sum_{x=0}^{n-1}lfloor frac{(a mod c)x + (b mod c)}{c} floor \ =&frac{n(n-1)}{2}lfloor frac{a}{c} floor + n lfloor frac{b}{c} floor + f(a mod c,b mod c, c, n) end{aligned} ]

    (code:)

    ll f(ll a,ll b,ll c,ll n)
    {
        if(n<=0) return 0;
        return n*(n-1)/2*(a/c)+n*(b/c)+f(c,(a*n+b)%c,a%c,(a%c*n+b%c)/c);
    }
    
  • 相关阅读:
    Java原始数据类型
    Java文件教程
    Java.util.ArrayDeque类
    Java 简介
    面向对象的程序设计
    Java8默认方法
    divide方法
    java.lang.Boolean.compareTo()方法实例
    AWT Button类
    Java的核心优势
  • 原文地址:https://www.cnblogs.com/lhm-/p/12229637.html
Copyright © 2011-2022 走看看