If a sequence of numbers A1, A2, ... , AN form an arithmetic progression A, calculate sum of
F(Ai), for L ≤ i ≤ R.
F(X) is defined as:
If X < 10 then F(X) = X.
Else F(X) = F(sum_of_digits(X)).
Example:
F(1378) =
F(1+3+7+8) =
F(19) =
F(1 + 9) =
F(10) =
F(1+0) =
F(1) = 1
Input
The first line of the input contains an integer T denoting the number of test cases.
Each test case is described in one line containing four integers: A1 denoting the first element of the
arithmetic progression A, D denoting the common difference between successive members of A, and L
and R as described in the problem statement.
Output
For each test case, output a single line containing one integer denoting sum of F(Ai).
Constraints
1 ≤ T ≤ 10^5
1 ≤ A1 ≤ 10^9
0 ≤ D ≤ 10^9
1 ≤ R ≤ 10^18
1 ≤ L ≤ R
Subtasks
Subtask 1: 0 ≤ D ≤ 100, 1 ≤ A1 ≤ 10^9, 1 ≤ R ≤ 100
Subtask 2: 0 ≤ D ≤ 109, 1 ≤ A1 ≤ 109, 1 ≤ R ≤ 10^6
Subtask 3: Original constraints
Example
Input:
2
1 1 1 3
14 7 2 4
Output:
6
12
Example case 2.
A = {14, 21, 28, 35, 42, 49, 56, 63, 70, 77, ...}
A2 = 21
A3 = 28
A4 = 35
F(A2) = 3
F(A3) = 1
F(A4) = 8
3+1+8=12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
unsigned long long t,a,d,l,r;
unsigned long long q[9]={0,0,0,0,0,0,0,0,0};
unsigned long long sum[9]={0,0,0,0,0,0,0,0,0};
unsigned long long y,total;
cin>>t;
while(t--)
{
cin>>a>>d>>l>>r;
if(d%3!=0 && d%9!=0)
{
for(int i=0;i<9;i++)
{
if(a<10)
{
q[i]=a;
}
else
{
while(a>=10)
{
unsigned long long sum1=0;
while(a!=0)
{
y=a%10;
sum1=sum1+y;
a=a/10;
}
a=sum1;
if(a<10)
{
q[i]=a;
break;
}
else continue;
}
a=a+d;
}
}
sum[0]=q[0];
sum[1]=sum[0]+q[1];
sum[2]=sum[1]+q[2];
sum[3]=sum[2]+q[3];
sum[4]=sum[3]+q[4];
sum[5]=sum[4]+q[5];
sum[6]=sum[5]+q[6];
sum[7]=sum[6]+q[7];
sum[8]=sum[7]+q[8];
total=((r/9)*sum[8] + sum[r%9-1])-(((l-1)/9)*sum[8] + sum[(l-1)%9-1]);
cout<<total<<"\n";
}
if(d%3==0 && d%9!=0)
{
for(int i=0;i<3;i++)
{
if(a<10)
{
q[i]=a;
}
else
{
while(a>=10)
{
unsigned long long sum1=0;
while(a!=0)
{
y=a%10;
sum1=sum1+y;
a=a/10;
}
a=sum1;
if(a<10)
{
q[i]=a;
break;
}
else continue;
}
a=a+d;
}
}
sum[0]=q[0];
sum[1]=q[0]+q[1];
sum[2]=q[0]+q[1]+q[2];
total=((r/3)*sum[2] + sum[r%3-1])-(((l-1)/3)*sum[2] + sum[(l-1)%3-1]);
cout<<total<<"\n";
}
if(d%9==0)
{
if(a<10)
{
q[0]=a;
}
else
{
while(a>=10)
{
unsigned long long sum1=0;
while(a!=0)
{
y=a%10;
sum1=sum1+y;
a=a/10;
}
a=sum1;
if(a<10)
{
q[0]=a;
break;
}
else continue;
}
}
total=q[0]*(r-l+1);
cout<<total<<"\n";
}
}
return 0;
}
|
In this code i have used the idea that the numbers in the A.P. are following a certain pattern because the result F(X) lies between 1 to 9.
in case the common differnce is a mutiple of 3 but not a mutliple of 3 then the numbers repeat after 3 numbers.If the common diff. is a multiple of 9 then the numbers show the same result every time.
else the numbers repeat themselves after every 9 numbers.