比赛 20120316 评测结果 TAAAAAAATA
题目名称 求和B 最终得分 80
用户昵称 Truth.Cirno 运行时间 0.000 s
代码语言 C++ 内存使用 0.00 MiB
提交时间 2012-03-16 21:58:03
显示代码纯文本
#include <cstdio>
#include <memory.h>
using namespace std;

unsigned int m,len,num[40];

void plus(int x,int k)
{
	unsigned int lent=0;
	while (x)
	{
		lent++;
		num[lent]+=x%k;
		x/=k;
	}
	if (lent>len)
		len=lent;
}

unsigned int power(int num,int level)
{
	unsigned int temp=1;
	while (level)
	{
		temp*=num;
		level--;
	}
	return(temp);
}

void printit(void)
{
	unsigned long long i,ans=0;
	for (i=1;i<=len;i++)
		ans+=num[i]*power(m,i-1);
	printf("%d\n",ans);
}

int main(void)
{
	freopen("sumb.in","r",stdin);
	freopen("sumb.out","w",stdout);
	unsigned int i,j,k,t,n,x,y;
	scanf("%d",&t);
	for (i=1;i<=t;i++)
	{
		scanf("%d%d",&n,&m);
		len=0;
		memset(num,0,sizeof(num));
		for (j=1;j<=n;j++)
		{
			scanf("%d%d",&x,&y);
			for (k=x;k<=y;k++)
				plus(k,m);
		}
		for (j=1;j<=len;j++)
			num[j]%=m;
		printit();
	}
	return(0);
}