栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 2344 Toral Tickets

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 2344 Toral Tickets

#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <cmath>#include <set>#include <vector>#include <cstdlib>#define M0(a) memset(a, 0, sizeof(a))#define MXN 100010#define Inf 0xfffff#define eps 1e-8using namespace std;struct oo{int x, y;} b[1051];int a[22][22], c[22][22], d[22][22];int n , m, G;bool bo[22 * 22];string ans;string num[420];string sG;int flag;inline void clear(string& a){ while(a.length()>0 && a[0]=='0') a.erase(0, 1); if(a == "") a = "0";} bool isBigger(string a, string b){ clear(a); clear(b); if(a.length() > b.length()) return true; if(a.length()==b.length() && a>=b) return true; return false; }string stringAddString(string a, string b){ while(a.length() < b.length())  a = '0' + a; while(a.length() > b.length()) b = '0' + b;  a = '0' + a; b = '0' + b; for(int i=a.length()-1; i>=0; i--){ a[i] = a[i] + b[i] - '0'; if(a[i] > '9'){ a[i] = a[i] - 10; a[i-1] += 1; } }  clear(a); return a; }string stringSubString(string a, string b){ bool aBigger = true; while(a.length() < b.length())  a = '0' + a; while(a.length() > b.length()) b = '0' + b;  if(a < b)  {  aBigger = false;  string buf = b;  b = a;  a = buf;  }  for(int i=a.length()-1; i>=0; i--){ if(a[i] >= b[i]){ a[i] = a[i] - (b[i] - '0'); }else{ a[i] = a[i] + 10; a[i-1] -= 1; a[i] = a[i] - (b[i] - '0'); } } clear(a); if(!aBigger)  a = '-' + a; return a; }string stringMultString(string a, string b){ string result = "0"; if(a.length() < b.length()){ string buf = a; a = b; b = buf; }  for(int i=b.length()-1; i>=0; i--){ for(int j=0; j<b[i]-'0'; j++){ result = stringAddString(result, a); } a = a + '0'; } clear(result); return result; }string stringDivString(string a, string b){ clear(a); clear(b); if(b == "0") return "Error!"; string result = ""; string remainder = ""; for(int i=0; i<a.length(); i++){ remainder = remainder + a[i]; result = result + '0'; while(isBigger(remainder, b)){ result[result.length()-1]++; remainder = stringSubString(remainder, b); } } clear(result); return result;}void pre_pow2(){ num[0] = "1"; for (int i = 1; i <= 400; ++i){ num[i] = stringMultString(num[i-1], "2"); } }void swap(int & a, int & b){ int c = a;  a = b; b = c;}void init(){ M0(a); M0(b); M0(c); int cnt = 0; for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) a[i][j] = cnt++;}void work(int xt, int yt){ int x, k; for (int i = 1; i <= n; ++i)  for (int j = 1; j <= m; ++j){ k = i + xt; if (k > n) k -= n; d[k][j] = a[i][j]; } for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j){ k = j + yt; if (k > m) k -= m; a[i][k] = d[i][j]; }}void count(){ int cnt = 0; M0(bo); int x, y, nowx, nowy; for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) if (!bo[c[i][j]]){ ++cnt; x = i;  y = j; while (!bo[c[x][y]]){ bo[c[x][y]] = true; nowx = x;  nowy = y; x = c[nowx][nowy] / m; y = c[nowx][nowy] % m; } }  ans = stringAddString(ans, num[cnt]);}void work(){ for (int i = 0; i < n; ++i)  for (int j = 0; j < m; ++j){ M0(c); for (int k = 0; k < n; ++k) for (int l = 0; l < m; ++l) c[(i + k) % n][(j + l) % m] = a[k][l];  count(); }}void rorate(){ int dd; M0(c); for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) c[m-j-1][i] = a[i][j]; swap(n, m); for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) a[i][j] = c[i][j];}void changes(int g){ if (g == 0) return; changes(g / 10); sG.push_back((g % 10) + 48);}void solve(){ ans = "0"; sG.clear(); G = n * m * 2;  if (n == m) G <<= 1; work(); rorate(); if (n == m) work(); rorate(); work(); rorate(); changes(G); if (n == m) work(); cout << stringDivString(ans, sG) << endl;}int main(){ flag = 0; pre_pow2(); while (scanf("%d%d", &n, &m) != EOF){ if (n < m) swap(n, m); init(); solve();  ++flag; }}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/372653.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号