[组合计数] BZOJ 3505 [Cqoi2014]数三角形
2016-07-13 16:22
190 查看
减去共线的就好了
线上的整点用gcd
#include<cstdio>
#include<cstdlib>
using namespace std;
typedef long long ll;
ll Gcd(ll a,ll b){
if (a<b) return Gcd(b,a);
return b?Gcd(b,a%b):a;
}
ll n,m;
ll ans;
int main()
{
freopen("t.in","r",stdin);
freopen("t.out","w",stdout);
scanf("%lld%lld",&n,&m);
ans=((n+1)*(m+1))*((n+1)*(m+1)-1)*((n+1)*(m+1)-2)/6;
for (int i=0;i<=n;i++)
for (int j=0;j<=m;j++)
{
if (!i && !j) continue;
ll t=(n-i+1)*(m-j+1)*(Gcd(i,j)-1);
ans-=t;
if (i && j) ans-=t;
}
printf("%lld\n",ans);
return 0;
}
线上的整点用gcd
#include<cstdio>
#include<cstdlib>
using namespace std;
typedef long long ll;
ll Gcd(ll a,ll b){
if (a<b) return Gcd(b,a);
return b?Gcd(b,a%b):a;
}
ll n,m;
ll ans;
int main()
{
freopen("t.in","r",stdin);
freopen("t.out","w",stdout);
scanf("%lld%lld",&n,&m);
ans=((n+1)*(m+1))*((n+1)*(m+1)-1)*((n+1)*(m+1)-2)/6;
for (int i=0;i<=n;i++)
for (int j=0;j<=m;j++)
{
if (!i && !j) continue;
ll t=(n-i+1)*(m-j+1)*(Gcd(i,j)-1);
ans-=t;
if (i && j) ans-=t;
}
printf("%lld\n",ans);
return 0;
}
相关文章推荐
- Qt学习之路(52): 拖放技术之一
- Android自定义开关(可滑动,点击)
- HDU4497GCD and LCM ——算数基本定理
- WDS+MDT部署Windows7操作系统6—创建任务序列
- iOS应用内支付(IAP)详解
- 用C#用C#实现窗体在规定时间弹出,例如:10:00.弹出后关闭。并在5分钟后再次弹出。5次后停止。最好有具体代码实现窗体在规定时间弹出,例如:10:00.弹出后关闭。并在5分钟后再次弹出。5次后停止。最好有具体代码
- C#多线程学习之(五)使用定时器进行多线程的自动管理
- iOS 第三方框架大全
- WDS+MDT部署Windows7操作系统—部署操作系统
- Server 2012 R2 RMS文档管理系统
- JZOJ1421. 二叉树
- 批量导出AD账号
- 2015年河北省成人高校招生考试准考证打印及查询
- MDT客户端PXE不能启动
- sql 语句的拼接
- mysql、oracle、sqlserver中只显示前几条记录的语法
- 历年软考真题大集合
- 使用组策略将用户文件夹重定向到D盘
- 投资什么,收益最高?
- C#中OpenFileDialog获取文件名和文件路径的常用方法