【CF963C】Cutting Rectangle(数论,构造,map)

题意:

 

 思路:考虑构造最小的单位矩形然后平铺

单位矩形中每种矩形的数量可以根据比例算出来,为c[i]/d,其中d是所有c[i]的gcd,如果能构造成功答案即为d的因子个数

考虑如果要将两种矩形放在同一行那他们的w一定相等,且对于每一行h全部出现过并且比例相当

具体实现的时候用map套vector,发现map有好多写法

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 typedef long long ll;
  4 typedef unsigned int uint;
  5 typedef unsigned long long ull;
  6 typedef long double ld;
  7 typedef pair<int,int> PII;
  8 typedef pair<ll,ll> Pll;
  9 typedef vector<int> VI;
 10 typedef vector<PII> VII;
 11 typedef pair<ll,ll>P;
 12 #define N  2000000+10
 13 #define M  200000+10
 14 #define INF 1e9
 15 #define fi first
 16 #define se second
 17 #define MP make_pair
 18 #define pb push_back
 19 #define pi acos(-1)
 20 #define mem(a,b) memset(a,b,sizeof(a))
 21 #define rep(i,a,b) for(int i=(int)a;i<=(int)b;i++)
 22 #define per(i,a,b) for(int i=(int)a;i>=(int)b;i--)
 23 #define lowbit(x) x&(-x)
 24 #define Rand (rand()*(1<<16)+rand())
 25 #define id(x) ((x)<=B?(x):m-n/(x)+1)
 26 #define ls p<<1
 27 #define rs p<<1|1
 28 #define fors(i) for(auto i:e[x]) if(i!=p)
 29 
 30 const int MOD=998244353,inv2=(MOD+1)/2;
 31       //int p=1e4+7;
 32       //double eps=1e-6;
 33       int dx[4]={-1,1,0,0};
 34       int dy[4]={0,0,-1,1};
 35 
 36 int read()
 37 {
 38    int v=0,f=1;
 39    char c=getchar();
 40    while(c<48||57<c) {if(c=='-') f=-1; c=getchar();}
 41    while(48<=c&&c<=57) v=(v<<3)+v+v+c-48,c=getchar();
 42    return v*f;
 43 }
 44 
 45 struct node
 46 {
 47     ll w,h,c;
 48 }a[N];
 49 
 50 map<ll,vector<Pll>> mp;
 51 vector<Pll> c;
 52 
 53 ll readll()
 54 {
 55    ll v=0,f=1;
 56    char c=getchar();
 57    while(c<48||57<c) {if(c=='-') f=-1; c=getchar();}
 58    while(48<=c&&c<=57) v=(v<<3)+v+v+c-48,c=getchar();
 59    return v*f;
 60 }
 61 
 62 ll gcd(ll x,ll y)
 63 {
 64     if(y==0) return x;
 65     return gcd(y,x%y);
 66 }
 67 
 68 int isok()
 69 {
 70     for(auto &element:mp)
 71     {
 72         if(element.se.size()!=c.size()) return 0;
 73         int n=c.size();
 74         ll d=0;
 75         rep(i,0,n-1) d=gcd(d,element.se[i].se);
 76         rep(i,0,n-1)
 77         {
 78             element.se[i].se/=d;
 79             if(element.se[i]!=c[i]) return 0;
 80         }
 81     }
 82     return 1;
 83 }
 84 
 85 int main()
 86 {
 87     //freopen("1.in","r",stdin);
 88     //freopen("1.out","w",stdout);
 89     int n=read();
 90     ll K=0;
 91     rep(i,1,n)
 92     {
 93         a[i].w=readll(),a[i].h=readll(),a[i].c=readll();
 94         K=gcd(K,a[i].c);
 95         mp[a[i].w].pb(Pll(a[i].h,a[i].c));
 96     }
 97     for(auto &element:mp)
 98     {
 99         for(auto &x:element.se) x.se/=K;
100         sort(element.se.begin(),element.se.end());
101     }
102 
103     c=mp.begin()->se;
104     ll d=0;
105     for(auto &x:c) d=gcd(d,x.se);
106     for(auto &x:c) x.se/=d;
107     if(!isok())
108     {
109         printf("0\n");
110         return 0;
111     }
112     int ans=0;
113     int t=sqrt(K);
114     rep(i,1,t)
115      if(K%i==0)
116      {
117          ans++;
118          if(1ll*i*i!=K) ans++;
119      }
120     printf("%d\n",ans);
121     return 0;
122 }

 

posted on 2019-11-26 20:14  myx12345  阅读(332)  评论(0编辑  收藏  举报

导航