UVALive 3295 Counting Triangles
2016-07-25 17:53
417 查看
UVALive 3295 Counting Triangles
链接:UVALive 3295题意:给出一个M*N的矩阵,问取其任意三点最多能取多少不完全重叠的三角形?
思路:这题与UVALive 3720 Highways很相似,描述好复杂,组合数学+dp。其实就是先求出组合数C,然后减去重复的和无法组成三角形的情况,重复情况用dp数组递推出来,递推公式与Highways也很相似。
下面是完整代码:
#include <cstdio> #include <iostream> #include <cstring> #include <cstdlib> #include <cmath> #include <set> #include <algorithm> #include <map> #include <bitset> #include <vector> #include <queue> #include <stack> #include <utility> #include <functional> #include <sstream> #include <list> #include <complex> #include <ctime> using namespace std; #define ll long long #define ull unsigned long long const int MAXN=2000005; const int INF=987654323; const int MOD=1e9+7; ll m,n,t,ans; ll C[MAXN][4],dp[1005][1005]; int gcd(int a,int b) { return b==0?a:gcd(b,a%b); } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); memset(C,0,sizeof(C)); for(ll i=0;i<MAXN;i++) C[i][0]=1; for(ll i=0;i<MAXN;i++) for(ll j=1;j<4;j++) C[i][j]=C[i-1][j]+C[i-1][j-1]; for(ll i=2;i<1005;i++) for(ll j=2;j<1005;j++) dp[i][j]=dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]+gcd(i,j)-1; for(ll i=2;i<1005;i++) for(ll j=2;j<1005;j++) dp[i][j]+=dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]; t=1; scanf("%lld%lld",&m,&n); while(m||n){ ans=C[(n+1)*(m+1)][3]-C[n+1][3]*(m+1)-C[m+1][3]*(n+1)-(dp[m] <<1); printf("Case %lld: %lld\n",t++,ans); scanf("%lld%lld",&m,&n); } return 0; }
相关文章推荐
- 详解Android应用中屏幕尺寸的获取及dp和px值的转换
- 基于Android中dp和px之间进行转换的实现代码
- Android中dip、dp、sp、pt和px的区别详解
- LFC1.0.0 版本发布
- Android dpi,dip,dp的概念以及屏幕适配
- Android px、dp、sp之间相互转换
- HP data protector软件学习1--基本角色与基本工作流程
- HP data protector软件学习2--软件组成与界面介绍
- android中像素单位dp、px、pt、sp的比较
- Android对px和dip进行尺寸转换的方法
- Android根据分辨率进行单位转换-(dp,sp转像素px)
- android 尺寸 dp,sp,px,dip,pt详解
- DP问题各种模型的状态转移方程
- POJ-1695-Magazine Delivery-dp
- nyoj-1216-整理图书-dp
- TYVJ1193 括号序列解题报告
- 对DP的一点感想
- TYVJ上一些DP的解题报告
- soj1005. Roll Playing Games
- 01背包问题