Most of the times, the students of Computer Science & Engineering of BUET deal with bogus, tough and
very complex formulae. That is why, sometimes, even for a easy problem they think very hard and make
the problem much complex to solve. But, the team members of the team “BUET PESSIMISTIC”
are the only exceptions. Just like the opposite manner, they treat every hard problem as easy and so
cannot do well in any contest. Today, they try to solve a series but fail for treating it as hard. Let
them help.
Input
Just try to determine the answer for the following series
∑
N
i=1
iAi
You are given the value of integers N and A (1 ≤ N ≤ 150, 0 ≤ A ≤ 15).
Output
For each line of the input, your correct program should output the integer value of the sum in separate
lines for each pair of values of N and A.
Sample Input
3 3
4 4
Sample Output
102
1252
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<ctype.h>
#include<stack>
#include<math.h>
#include <string>
#include<algorithm>
#include <iterator>
#include <stdlib.h>
using namespace std;
typedef unsigned long long ULL;
const int N=500;
struct node
{
int num[N];
int cnt;
}a,b;
int muti(int k)
{
for(int i=0;i<a.cnt;i++)
a.num[i]*=k;
int i;
for(i=0;i<a.cnt;i++)
{
a.num[i+1]+=a.num[i]/10;
a.num[i]%=10;
}
//剩余之后
while(a.num[i])
{
a.num[i+1]+=a.num[i]/10;
a.num[i]%=10;
i++;
}
//去零
while(i>=0&&!a.num[i]) i--;
a.cnt=i+1;
}
node add(node a,node b)
{
node temp;
memset(temp.num,0,sizeof(temp.num));
temp.cnt=0;
for(int i=0,j=0,g=0;;i++,j++)
{
if(g==0&&i>=a.cnt&&j>=b.cnt) break;
int t=g;
if(i<a.cnt) t+=a.num[i];
if(j<b.cnt) t+=b.num[i];
temp.num[temp.cnt++]=t%10;
g=t/10;
}
return temp;
}
int main()
{
int N,A;
while(scanf("%d%d",&N,&A)!=EOF)
{
if(A==0)
{
printf("0\n");
continue;
}
memset(a.num,0,sizeof(a.num));
memset(b.num,0,sizeof(b.num));
b.cnt=a.cnt=0;
for(int i=1;i<=N;i++)
{
memset(a.num,0,sizeof(a.num));
a.cnt=1,a.num[0]=1;
for(int j=1;j<=i;j++)
muti(A);
muti(i);
b=add(a,b);
}
for(int i=b.cnt-1;i>=0;i--)
printf("%d",b.num[i]);
printf("\n");
}
return 0;
}