Digital root
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 98304/98304 K (Java/Others)Total Submission(s): 1056 Accepted Submission(s): 328
Problem Description
The digital root (also repeated digital sum) of a number is the (single digit) value obtained by an iterative process of summing digits, on each iteration using the result from the previous iteration to compute a digit sum. The process continues until a single-digit number is reached.
For example, the digital root of 65536 is 7, because 6+5+5+3+6=25 and 2+5=7.
The digital root of an interval is digital root of the sum of all numbers in that interval.
Consider a sequence A 1,A 2,A 3……A n, your task is to answer some queries[l,r]. For each query, tell the biggest five different digital roots among all continuous subintervals of interval[l,r]
For example, the digital root of 65536 is 7, because 6+5+5+3+6=25 and 2+5=7.
The digital root of an interval is digital root of the sum of all numbers in that interval.
Consider a sequence A 1,A 2,A 3……A n, your task is to answer some queries[l,r]. For each query, tell the biggest five different digital roots among all continuous subintervals of interval[l,r]
Input
In the first line of the input , we have one integer T indicating the number of test cases. (1 <= T <= 10) Then T cases follows. For each test case, the first line is an integer n indicating the length of sequence(1<=n<=100000); following one line containing n integer A
i(0<=A
i<=1000000000); the third line is an integer Q indicating the number of queries(1<=Q<=100000); following Q lines, each line indicating one query [l,r],(1<=l<=r<=n).
Output
For each test case, first you should print "Case #x:" in a line, where x stands for the case number started with 1. Then for each query output a line contains five integer indicating the answers(if the kind of digital root is smaller than five, output -1 to complete). Leave an empty line between test cases.
Sample Input
1 5 101 240 331 4 52 3 1 3 4 5 1 5
Sample Output
Case #1: 8 7 6 4 2 7 4 2 -1 -1 9 8 7 6 4HintFor the first query, [1,3] has six subintervals: [1,1],[2,2],[3,3],[1,2],[2,3],[1,3]. Interval sums are 101,240,331,341,571,672, correspondence digital roots are 2,6,7 ,8,4,6,the most biggest five are 8,7,6,4,2
Author
L7we@UESTC_Alchemist
Source
Recommend
zhuyuanchen520
好蛋疼的线段树题。
维护前面,中间和后面三个部分。
数字根就是一个数每一位加起来mod9,如果结果是0数字根就是9,不然就是mod9的值,注意判断原来的数是不是就是0.
需要用状态压缩下10个数出现的情况,不然会tle。
代码
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
typedef struct
{
int l,r,sum;
int lsum,rsum,midsum;
}Tree;
Tree tree[500005];
int a[100005];
int c[2000][2000];
int ans,rans;
int Count(int t)
{
if (t==0) return 0;
int s=0;
while(t)
{
s+=t%10;
t/=10;
}
s%=9;
if (s==0) return 9;
return s;
}
int Pre()
{
int i,j,x,y;
for (i=0;i<1024;i++)
{
for (j=i;j<1024;j++)
{
c[i][j]=0;
for (x=0;x<10;x++)
{
if ((i & (1<<x))==0) continue;
for (y=0;y<10;y++)
{
if ((j & (1<<y))==0) continue;
c[i][j]=(c[i][j]|(1<<Count(x+y)));
}
}
c[j][i]=c[i][j];
}
}
}
void Build(int t,int l,int r)
{
tree[t].l=l;
tree[t].r=r;
if (l==r)
{
tree[t].sum=a[l];
tree[t].lsum=(1<<a[l]);
tree[t].rsum=(1<<a[l]);
tree[t].midsum=(1<<a[l]);
return;
}
int mid=(l+r)/2;
Build(2*t+1,l,mid);
Build(2*t+2,mid+1,r);
tree[t].sum=Count(tree[2*t+1].sum+tree[2*t+2].sum);
tree[t].lsum=(tree[2*t+1].lsum | c[1<<tree[2*t+1].sum][tree[2*t+2].lsum]);
tree[t].rsum=(tree[2*t+2].rsum | c[tree[2*t+1].rsum][1<<tree[2*t+2].sum]);
tree[t].midsum=(tree[2*t+1].midsum | tree[2*t+2].midsum | c[tree[2*t+1].rsum][tree[2*t+2].lsum]);
}
void Query(int t,int x,int y)
{
int l,r;
l=tree[t].l;
r=tree[t].r;
if (l==x && r==y)
{
ans=(ans | tree[t].midsum | c[rans][tree[t].lsum]);
rans=(c[rans][1<<tree[t].sum] | tree[t].rsum);
return;
}
int mid=(l+r)/2;
if (mid>=x) Query(2*t+1,x,min(y,mid));
if (mid<y) Query(2*t+2,max(x,mid+1),y);
}
int main()
{
int T,i,j,n,x,y,m,s,cnt=1;
Pre();
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for (i=0;i<n;i++)
{
scanf("%d",&a[i]);
a[i]=Count(a[i]);
}
Build(0,0,n-1);
scanf("%d",&m);
printf("Case #%d:\n",cnt++);
while(m--)
{
scanf("%d%d",&x,&y);
ans=rans=0;
Query(0,x-1,y-1);
s=0;
for (i=9;i>=0;i--)
{
if (ans>=(1<<i))
{
ans-=(1<<i);
if (s==0) printf("%d",i);
else printf(" %d",i);
s++;
if (s==5) break;
}
}
for (i=s;i<5;i++)
{
printf(" -1");
}
printf("\n");
}
if (T>0) printf("\n");
}
return 0;
}