Link: http://pat.zju.edu.cn/contests/ds/2-06
Given a number a (1 <= A <= 9) and a non-negative integer N (0 <= n <= 100000), calculate the sum of the series S = a + AA + AAA +... + AA... A (n ). For example, if a = 1, n = 3, S = 1 + 11 + 111 = 123.
Input format description:
Enter the number a and the non-negative integer n.
Output format description:
Output the value of S in the sum of n columns.
Sample input and output:
Serial number |
Input |
Output |
1 |
1 3 |
123 |
2 |
6 100 |
7407407407407407407407407407407407407407407407407407407407407407407407407407407407407407407407407340 |
3 |
1 0 |
0 |
The Code is as follows:
#include <cstdio>#include <cmath>int a[100017];int main(){ int A, N; while(~scanf("%d%d",&A,&N)) { if(N == 0) { printf("0\n"); continue; } int tt = 0,p = 0; int j = 0; for(int i = N; i >= 1; i--) { tt = A*i+p; p = tt/10; a[j++] = tt%10; } if(p > 0) { a[j++] = p; } for(int i = j-1; i >= 0; i--) { printf("%d",a[i]); } printf("\n"); } return 0;}
2-06. Sum of series (20) (zjupat mathematics)