SPOJ - Counting Triangles Problem Solution
Solution:-
using namespace std;
#define MAX 1000010
long long up[MAX],down[MAX];
void init()
{
up[1]=1;
up[2]=4;
down[2]=1;
for(int i=3;i<=MAX;i++)
{
up[i]=2*up[i-1]+i-up[i-2];
down[i]=up[i-1]-down[i-1];
}
}
int main()
{
init();
int tc,num;;
cin>>tc;
while(tc--)
{
cin>>num;
cout<<up[num]+down[num]<<endl;
}
return 0;
}
No comments:
Post a Comment