[Gym102135][G - Digital characteristic]2017-2018 8th BSUIR Open Programming Contest

description

给定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+bima+bim)
= ∑ 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(mma+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(mf(b,a,m,k1))%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)=0ncai+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>=cb>=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)=0na/ci+b/c+ca%ci+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/cn(n+1)/2+b/c(n+1)+f(a%c,b%c,c,n)
当 a < c 且 b < c 时 当aa<cb<c
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=0nj=0cai+b11
= ∑ 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=0can+b1i=0n1(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=0can+b1i=0n1(j<cai+bc+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=0can+b1i=0n1(i>acjb+c1)
= ∑ 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=0can+b1nacjb+c1
= 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) =ncan+bf(c,b+c1,can+b1)
同理可以求出 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)=icai+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+b2(平方这里需要有一个转换) n 2 = 2 ∗ ∑ i = 0 n i − n n^2=2*\sum_{i=0}^{n}i-n n2=2i=0nin

#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);
	}
} 

你可能感兴趣的:(icpc,数论,Gym102135,类欧几里得,数论)