zoukankan      html  css  js  c++  java
  • Kattis

    题意:计算$sumlimits_{i=1}^n[(p{cdot }i)mod{q}]$

    类欧模板题,首先作转化$sumlimits_{i=1}^n[(p{cdot}i)mod{q}]=sumlimits_{i=1}^n[p{cdot}i-leftlfloorfrac{p{cdot}i}{q} ight floor{cdot}q]$,然后只要能快速计算$sumlimits_{i=1}^nleftlfloorfrac{p{cdot}i}{q} ight floor$就行了。

    记$f(a,b,c,n)=sumlimits_{i=0}^nleftlfloorfrac{ai+b}{c} ight floor$

    则有$f(a,b,c,n)=left{egin{matrix}egin{aligned}&(n+1)leftlfloorfrac{b}{c} ight floor,a=0\&f(a\%c,b\%c,c,n)+frac{n(n+1)}{2}leftlfloorfrac{a}{c} ight floor+(n+1)leftlfloorfrac{b}{c} ight floor,a>=c:or:b>=c\&nleftlfloorfrac{an+b}{c} ight floor-f(c,c-b-1,a,leftlfloorfrac{an+b}{c} ight floor-1),othersend{aligned}end{matrix} ight.$

    由于递推过程类似欧几里得法求gcd,因此称作类欧~~

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 typedef long long ll;
     4 const int N=1e5+10;
     5 int p,q,n;
     6 ll f(ll a,ll b,ll c,ll n) {
     7     if(!a)return (n+1)*(b/c);
     8     if(a>=c||b>=c)return f(a%c,b%c,c,n)+n*(n+1)/2*(a/c)+(n+1)*(b/c);
     9     ll m=(a*n+b)/c;
    10     return n*m-f(c,c-b-1,a,m-1);
    11 }
    12 int main() {
    13     int T;
    14     for(scanf("%d",&T); T--;) {
    15         scanf("%d%d%d",&p,&q,&n);
    16         printf("%lld
    ",(ll)n*(n+1)/2*p-f(p,0,q,n)*q);
    17     }
    18     return 0;
    19 }
  • 相关阅读:
    git忽略已提交过的文件方法
    去除git版本控制
    写博客的初衷
    Substring with Concatenation of All Words
    Course Schedule
    Reverse Words in a String--not finished yet
    Repeated DNA Sequences
    Maximum Product of Word
    Odd Even Linked List
    Reorder List
  • 原文地址:https://www.cnblogs.com/asdfsag/p/11393783.html
Copyright © 2011-2022 走看看