给定a,b,m,k,产生长度为k的数组,a1=a%m,ai=(ai-1+b)%m(i>1),将这k个数首尾相接形成一个新数,问这个数的f函数的值
‘’‘solution’’’
首先f(x)可以等价于询问x%9的值,由于10的幂模9都为1,x%9= ( ∑ ( a + b i ) % m ) (\sum{(a+bi)\%m})%9 (∑(a+bi)%m)
= ( ∑ a + b i − ⌊ a + b i m ⌋ ∗ m ) =(\sum{a+bi-\lfloor{\dfrac{a+bi}{m}}\rfloor*m})%9 =(∑a+bi−⌊ma+bi⌋∗m)
= ∑ a + b i % 9 − ( m ∗ ∑ ⌊ a + b i m ⌋ ) % 9 =\sum{a+bi}\%9-(m*\sum{\lfloor{\dfrac{a+bi}{m}}\rfloor})\%9 =∑a+bi%9−(m∗∑⌊ma+bi⌋)%9
= ∑ a + b i % 9 − ( m ∗ f ( b , a , m , k − 1 ) ) % 9 =\sum{a+bi}\%9-(m*f(b,a,m,k-1))\%9 =∑a+bi%9−(m∗f(b,a,m,k−1))%9
类 欧 几 里 得 推 导 : f ( a , b , c , n ) = ∑ 0 n ⌊ a i + b c ⌋ 类欧几里得推导:f(a,b,c,n)=\sum^{n}_{0}{\lfloor{\dfrac{ai+b}{c}}\rfloor} 类欧几里得推导:f(a,b,c,n)=0∑n⌊cai+b⌋
当 a = 0 时 : 当a=0时: 当a=0时:
f ( a , b , c , n ) = ⌊ b / c ⌋ ∗ ( n + 1 ) f(a,b,c,n)=\lfloor{b/c}\rfloor*(n+1) f(a,b,c,n)=⌊b/c⌋∗(n+1)
当 a > = c 或 b > = c 时 : 当a>=c或b>=c时: 当a>=c或b>=c时:
f ( a , b , c , n ) = ∑ 0 n ⌊ a / c ⌋ ∗ i + ⌊ b / c ⌋ + ⌊ a % c ∗ i + b % c c ⌋ f(a,b,c,n)=\sum_{0}^{n}{\lfloor{a/c}\rfloor*i+\lfloor{b/c}\rfloor+\lfloor{\dfrac{a\%c*i+b\%c}{c}}\rfloor} f(a,b,c,n)=0∑n⌊a/c⌋∗i+⌊b/c⌋+⌊ca%c∗i+b%c⌋
= ⌊ a / c ⌋ n ∗ ( n + 1 ) / 2 + ⌊ b / c ⌋ ∗ ( n + 1 ) + f ( a % c , b % c , c , n ) =\lfloor{a/c}\rfloor n*(n+1)/2+\lfloor{b/c}\rfloor*(n+1)+f(a\%c,b\%c,c,n) =⌊a/c⌋n∗(n+1)/2+⌊b/c⌋∗(n+1)+f(a%c,b%c,c,n)
当 a < c 且 b < c 时 当a
f ( a , b , c , n ) = ∑ i = 0 n ∑ j = 0 ⌊ a i + b c ⌋ − 1 1 f(a,b,c,n)=\sum_{i=0}^{n}{\sum_{j=0}^{\lfloor{\dfrac{ai+b}{c}}\rfloor-1}1} f(a,b,c,n)=i=0∑nj=0∑⌊cai+b⌋−11
= ∑ j = 0 ⌊ a n + b c ⌋ − 1 ∑ i = 0 n 1 ( j < ⌊ a i + b c ⌋ ) =\sum_{j=0}^{\lfloor{\dfrac{an+b}{c}}\rfloor-1}{\sum_{i=0}^{n}1(j<\lfloor{\dfrac{ai+b}{c}}\rfloor)} =j=0∑⌊can+b⌋−1i=0∑n1(j<⌊cai+b⌋)
= ∑ j = 0 ⌊ a n + b c ⌋ − 1 ∑ i = 0 n 1 ( j < ⌈ a i + b − c + 1 c ⌉ ) =\sum_{j=0}^{\lfloor{\dfrac{an+b}{c}}\rfloor-1}{\sum_{i=0}^{n}1(j<\lceil{\dfrac{ai+b-c+1}{c}}\rceil)} =j=0∑⌊can+b⌋−1i=0∑n1(j<⌈cai+b−c+1⌉)
= ∑ j = 0 ⌊ a n + b c ⌋ − 1 ∑ i = 0 n 1 ( i > ⌊ c j − b + c − 1 a ⌋ ) =\sum_{j=0}^{\lfloor{\dfrac{an+b}{c}}\rfloor-1}{\sum_{i=0}^{n}1(i>\lfloor{\dfrac{cj-b+c-1}{a}}\rfloor)} =j=0∑⌊can+b⌋−1i=0∑n1(i>⌊acj−b+c−1⌋)
= ∑ j = 0 ⌊ a n + b c ⌋ − 1 n − ⌊ c j − b + c − 1 a ⌋ =\sum_{j=0}^{\lfloor{\dfrac{an+b}{c}}\rfloor-1}{n-\lfloor{\dfrac{cj-b+c-1}{a}}\rfloor} =j=0∑⌊can+b⌋−1n−⌊acj−b+c−1⌋
= n ∗ ⌊ a n + b c ⌋ − f ( c , − b + c − 1 , ⌊ a n + b c ⌋ − 1 ) =n*\lfloor{\dfrac{an+b}{c}}\rfloor-f(c,-b+c-1,\lfloor{\dfrac{an+b}{c}}\rfloor-1) =n∗⌊can+b⌋−f(c,−b+c−1,⌊can+b⌋−1)
同理可以求出 g ( a , b , c , n ) = ∑ i ∗ ⌊ a i + b c ⌋ g(a,b,c,n)=\sum{i*\lfloor{\dfrac{ai+b}{c}}\rfloor} g(a,b,c,n)=∑i∗⌊cai+b⌋
h ( a , b , c , n ) = ∑ ⌊ a i + b c ⌋ 2 h(a,b,c,n)=\sum{\lfloor{\dfrac{ai+b}{c}}\rfloor^2} h(a,b,c,n)=∑⌊cai+b⌋2(平方这里需要有一个转换) n 2 = 2 ∗ ∑ i = 0 n i − n n^2=2*\sum_{i=0}^{n}i-n n2=2∗∑i=0ni−n
#include
#define LL long long
#define fo(i,a,b) for(LL i=a;i<=b;i++)
#define rp(i,a,b) for(LL i=a;i>=b;i--)
#define tr(t,x) for(LL t=first[x];t;t=nex[t])
using namespace std;
const LL mo=9;
LL n,m,i,t,j,k,l,x,y,z,a,b,ans,T;
LL sqr(LL n){
return n*(n+1)/2%mo;
}
LL f(LL a,LL b,LL c,LL n){
if (!a) return (b/c)*(n+1)%mo;
if (a>=c || b>=c) return (f(a%c,b%c,c,n)+sqr(n)*(a/c)+(n+1)*(b/c))%mo;
LL t=(a*n+b)/c;
return (t%mo*n%mo-f(c,c-b-1,a,t-1)+mo)%mo;
}
int main(){
//freopen("data.in","r",stdin);
scanf("%lld",&T);
while (T--){
scanf("%lld%lld%lld%lld",&a,&b,&m,&n);n--;
a%=m;b%=m;
if ((!n || ( n && !b)) && !a){
printf("0\n"); continue;
}
ans=a*(n+1)%mo+sqr(n)*b%mo-m*f(b,a,m,n)%mo;
ans=(ans+mo)%mo;
if (!ans) ans=9;
printf("%lld\n",ans);
}
}