hdu5116(计数DP)

计数问题确实是短板qaq

这个可以先求出符合要求的L,可以通过枚举顶点来求,然后就是求n,m里面互质数的对数,这个其实可以暴力预处理(一开始还想着用反演)。。

然后把总数平方后再减去交叉部分就是答案了。。。

交叉的情况其实很少,就3种

一种是同一个顶点,这个直接减去该顶点的L的平方即可

一种是L的边重叠,这个可以枚举重叠中的顶点,然后一个是在该顶点能构成的L,一个是经过该顶点的L,直接预处理出经过该顶点的L之后乘起来减掉即可。。

一种是2个L的边交叉,只相交于一点,这个可以枚举交叉点,然后预处理横向经过该点的L和纵向经过该点的L,乘起来减掉就行了。。(顺便处理了情况2)

貌似不是很难。。就是思路放不开。。得多接触。。尤其是转化为去算交叉部分那里。。

 

 

/**
 *        ┏┓    ┏┓
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃  
 *        ┃   ━    ┃
 *        ┃ >   < ┃
 *        ┃       ┃
 *        ┃... ⌒ ...  ┃
 *        ┃       ┃
 *        ┗━┓   ┏━┛
 *          ┃   ┃ Code is far away from bug with the animal protecting          
 *          ┃   ┃   神兽保佑,代码无bug
 *          ┃   ┃           
 *          ┃   ┃        
 *          ┃   ┃
 *          ┃   ┃           
 *          ┃   ┗━━━┓
 *          ┃       ┣┓
 *          ┃       ┏┛
 *          ┗┓┓┏━┳┓┏┛
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛
 */ 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1<>1)
#define NM 205 
#define nm 1100005
#define pi 3.1415926535897931
using namespace std;
ll read(){
    ll x=0,f=1;char ch=getchar() ;
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}










int n,m,_x,_y,ca;
ll ans,d[NM][NM],f[NM][NM],g[NM][NM],b[NM][NM];
bool v[NM][NM];


int main(){
    m=200;
    inc(i,1,m)inc(j,1,m)b[i][j]=b[i][j-1]+(__gcd(i,j)==1);
    int _=read();while(_--){
	ans=0;mem(d);mem(f);mem(g);mem(v);
	n=read();
	inc(i,1,n){_x=read();_y=read();v[_x][_y]++;}
	inc(i,1,m)inc(j,1,m)if(v[i][j]){
	    int x=i,y=j;
	    inc(k,i+1,m)if(v[k][j])x=k;else break;
	    inc(k,j+1,m)if(v[i][k])y=k;else break;
	    inc(k,1,x-i)d[i][j]+=b[k][y-j];
	    ans+=d[i][j];
	    ll t=0;
	    dec(k,y,j+1)t+=b[k-j][x-i],f[i][k]+=t;
	    t=0;
	    dec(k,x,i+1)t+=b[k-i][y-j],g[k][j]+=t;
	}
	ans=sqr(ans);
	inc(i,1,m)inc(j,1,m){
	    ans-=f[i][j]*g[i][j]*2;
	    ans-=sqr(d[i][j]);
	    ans-=2*d[i][j]*(f[i][j]+g[i][j]);
	}
	printf("Case #%d: %lld\n",++ca,ans);
    }
    return 0;
}

 

 

 

 

 

Everlasting L

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)
Total Submission(s): 420    Accepted Submission(s): 170


 

Problem Description

Matt loves letter L.

A point set P is (a, b)-L if and only if there exists x, y satisfying:

P = {(x, y), (x + 1, y), . . . , (x + a, y), (x, y + 1), . . . , (x, y + b)}(a, b ≥ 1)

A point set Q is good if and only if Q is an (a, b)-L set and gcd(a, b) = 1.

Matt is given a point set S. Please help him find the number of ordered pairs of sets (A, B) such that:

hdu5116(计数DP)_第1张图片

 

 

Input

The first line contains only one integer T , which indicates the number of test cases.

For each test case, the first line contains an integer N (0 ≤ N ≤ 40000), indicating the size of the point set S.

Each of the following N lines contains two integers xi, yi, indicating the i-th point in S (1 ≤ xi, yi ≤ 200). It’s guaranteed that all (xi, yi) would be distinct.

 

 

Output

For each test case, output a single line “Case #x: y”, where x is the case number (starting from 1) and y is the number of pairs.

 

 

Sample Input

 

2 6 1 1 1 2 2 1 3 3 3 4 4 3 9 1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3

 

 

Sample Output

 
Case #1: 2 Case #2: 6

Hint

n the second sample, the ordered pairs of sets Matt can choose are: A = {(1, 1), (1, 2), (1, 3), (2, 1)} and B = {(2, 2), (2, 3), (3, 2)} A = {(2, 2), (2, 3), (3, 2)} and B = {(1, 1), (1, 2), (1, 3), (2, 1)} A = {(1, 1), (1, 2), (2, 1), (3, 1)} and B = {(2, 2), (2, 3), (3, 2)} A = {(2, 2), (2, 3), (3, 2)} and B = {(1, 1), (1, 2), (2, 1), (3, 1)} A = {(1, 1), (1, 2), (2, 1)} and B = {(2, 2), (2, 3), (3, 2)} A = {(2, 2), (2, 3), (3, 2)} and B = {(1, 1), (1, 2), (2, 1)} Hence, the answer is 6.

 

 

Source

2014ACM/ICPC亚洲区北京站-重现赛(感谢北师和上交)

 

 

Recommend

liuyiding

 

 

Statistic | Submit | Discuss | Note

你可能感兴趣的:(DP)