Sports

题目链接 : http://acm.hpu.edu.cn/problem.php?id=1184

   或者       http://acm.nyist.net/JudgeOnline/problem.php?pid=1319


题目意思 : 给定三个整数N,M,K,在1<=a<=N和1<=b<=M的约束下,存在多少个不同的整数对(a,b)使得a∗b可以被K整除? 数据范围:1<=T<=10000,1<=N,M<=109,1<=K<=50000。

思路:    我们可以对于题目中的 K 进行因子对分解,假设分解后数对为(ai,bi);     再排序;    

假设这个数对是 (x,y) 那么x或者y乘以一个整数肯定能被K整除;所以 会有这样的整数数对 有 (n/ai)*(m/bi) 个;

假设我们枚举按照ai的升序,bi 降序,那么对应符合条件的列数一定会减少,对应符合条件的行数一定会增多;

所以我们用 arr[i] 代表第i个因子对且不包含之前统计过列的数量,那么在计算arr[i]  的时候就需要减去与之前计算重复的哪一些 ? 所以,如果  ai 是 aj 的倍数 (ai%aj=0),那么 得到(bj%bi=0) 第i个数对与第j个数对的,所以i所在的列,那么一定是j统计过的,所以需要减去j那部分列;


 可能说的不太清楚,或者我理解不深,请看代码自己想一想或许就懂了。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cctype>
 4 #include <cmath>
 5 #include <set>
 6 #include <map>
 7 #include <list>
 8 #include <queue>
 9 #include <deque>
10 #include <stack>
11 #include <string>
12 #include <vector>
13 #include <iostream>
14 #include <algorithm>
15 #include <stdlib.h>
16 #include <time.h>
17 
18 using namespace std;
19 typedef long long LL;
20 const int INF=2e9+1e8;
21 const int MOD=1e9+7;
22 const int MAXSIZE=1e6+5;
23 const double eps=0.0000000001;
24 void fre()
25 {
26     freopen("in.txt","r",stdin);
27     freopen("out.txt","w",stdout);
28 }
29 #define memst(a,b) memset(a,b,sizeof(a))
30 #define fr(i,a,n) for(int i=a;i<n;i++)
31 
32 
33 const int MAXN=5e4+10;
34 int arr[MAXN],fac[MAXN],myindex;
35 void init(int k)
36 {
37     myindex=0;
38     for(int i=1;i<=sqrt(k);i++)
39         if(k%i==0) fac[myindex++]=i,fac[myindex++]=k/i;
40 }
41 int main(int argc,char *argv[])
42 {
43     int ncase,k;
44     scanf("%d",&ncase);
45     while(ncase--)
46     {
47         LL n,m;
48         scanf("%lld%lld%d",&n,&m,&k);
49         init(k);
50         sort(fac,fac+myindex);
51         LL ans=0;
52         for(int i=0;i<myindex;i++)
53         {
54             int a=fac[i],b=k/fac[i];
55             arr[i]=m/b; // 列数
56             for(int j=0;j<i;j++)
57                 if(a%fac[j]==0) arr[i]-=arr[j];
58                 //排除一些列数 : 如果 行号成倍数;就说明重复了;所以有 容斥
59             ans+=n/a*(arr[i]);
60         }
61         printf("%lld\n",ans);
62     }
63     return 0;
64 }
65 
66 /**************************************************/
67 /**             Copyright Notice                 **/
68 /**  writer: wurong                              **/
69 /**  school: nyist                               **/
70 /**  blog  : http://blog.csdn.net/wr_technology  **/
71 /**************************************************/

 

posted @ 2016-12-04 15:42  Code-dream  阅读(279)  评论(0编辑  收藏  举报