标签:
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5666
题意:有条直线x+y=n,求这条直线与坐标轴围成的三角形中包含了多少个整数点,不能在坐标轴上和直线上,让结果对p求余
很明显答案就是(1+2+3+...+a-2)%P = ((a-1)(a-2)/2)%p;
由于a和p的范围都比较大,直接乘会爆LL的,所以可以用矩阵快速幂的思想;
x*y = 2*x*y/2; y是偶数;
x*y = x + 2*x*y/2; y是奇数;
所以就有了一下代码:
#include <iostream>
#include <vector>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
using namespace std;
#define N 1050
#define PI 4*atan(1)
#define met(a, b) memset(a, b, sizeof(a))
typedef long long LL;
LL Mul(LL x, LL y, LL Mod)
{
if(y == 0) return 0;
LL ans = 2 * Mul(x, y/2, Mod)%Mod;
if(y%2)
ans = (ans+x)%Mod;
return ans;
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
LL ans, p, q, n, mod;
scanf("%I64d %I64d", &n, &mod);
p = n-1; q = n-2;
if(p&1)
ans = Mul(p, q/2, mod);
else
ans = Mul(p/2, q, mod);
printf("%I64d\n", ans);
}
return 0;
}
Segment---hdu5666(两个long long 数相乘对long long求余)
标签:
原文地址:http://www.cnblogs.com/zhengguiping--9876/p/5450681.html