codechef Flooring

Problem Description

Your task is simple.
You need to find the value of

.
As the value could be too large, output it modulo M.
Input

The first contains an integer T, denoting the number of the test cases.
Then there are T lines, each describing a single test case and contains two space separated integers N and M respectively.
Output

For each test case, output the value of summation modulo M on a separate line.

Constraints

1 ≤ M ≤ 100000
There are two types of datasets:
1 ≤ N ≤ 106 , 1 ≤ T ≤ 3000
106 ≤ N ≤ 1010 , 1 ≤ T ≤ 30

Example

Input:

1
4 1000

Output:

373

Explanation

14*4 + 24*2 + 34*1 + 44*1 = 373

题解

数学题折腾了一天……也是醉了。

首先向下取整的操作意味着【N/i】是成阶梯状的。那么我们可以用分块的思想,处理出每一块的和。但是不能暴力求和,这里就涉及到前缀和的思想。

给点提示:1^4+2^4+……+n^4=n(n+1)(2n+1)(3n^2+3n-1)/30,高二上数列好好学……

                    a mod p= [ (a*m) mod (p*m) ] / m     (m常数) 

记得ans要清零,为这个倒是把自家电脑上的问题给解决了。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
int T;
ll n,m,ans;
ll calcu(ll x,ll y)//n(n+1)(2n+1)(3n^2+3n-1)/30
{
	ll a,b,c,sum1,sum2,mod;
	mod=30*m;
	a=x%mod; b=(a*(a+1))%mod*(2*a+1)%mod; c=(3*a*(a+1)+mod-1)%mod;
	sum1=(b*c%mod)/30;
	a=y%mod; b=(a*(a+1))%mod*(2*a+1)%mod; c=(3*a*(a+1)+mod-1)%mod;
	sum2=(b*c%mod)/30;
	if(sum2-sum1<0) return sum2-sum1+m;
	return sum2-sum1;
}
void work()
{
	ll i,j;
	ans=0;
	for(i=1;i<=n;i=j+1)
	   {j=n/(n/i);
	    ans=(ans+calcu(i-1,j)*(n/i)%m)%m;
	   }
	printf("%lld\n",ans);
}
int main()
{
	scanf("%d",&T);
	while(T--)
	   {scanf("%lld%lld",&n,&m);
	    work();
	   }
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WishingBone plans to decorate his hexagon-shaped room with unique flooring patterns. First he divides it into triangles, and colors them with different colors. Now that he has got a satisfactory pattern, he uses small rugs to cover the floor. The rugs sold at shops are diamond-shaped; exact the shape of two adjacent triangles. He is not willing to cut any of them. Different colored rugs are labeled different price. And he does care about the price - he cares about the most expensive rugs that he should use to finish the covering. Input There are multiple tests, for each test: The first line is an integer specifying the side of his room (1 <= n <= 50). The following 2n lines with the pattern of the room, with upper-case letters denoting different colors. So there are at most 26 colors. Since the room is considerably large and sometimes needs pillars to support the ceiling, some triangles are marked with '.' rather than a letter. Such triangles are not to be covered. The following line contains one integer m (1 <= m <= 351) - number of rugs in the shop. The following m lines contains one type of rug (with two upper-case letters) and its price p (1 <= p <= 100000). There will not be duplicate items listed, that is, if AB is listed, BA will not appear. You may assume the number of each type to be infinite. Output One line for each test, the price of the most expensive rugs used. Of course, you have to minimize this value. If there is no solution to the problem, print a 0.
最新发布
06-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值