zoukankan      html  css  js  c++  java
  • 蓝桥 PREV-34 历届试题 矩阵翻硬币

      历届试题 矩阵翻硬币  
    时间限制:1.0s   内存限制:256.0MB
        
    问题描述
      小明先把硬币摆成了一个 n 行 m 列的矩阵。

      随后,小明对每一个硬币分别进行一次 Q 操作。

      对第x行第y列的硬币进行 Q 操作的定义:将所有第 i*x 行,第 j*y 列的硬币进行翻转。

      其中i和j为任意使操作可行的正整数,行号和列号都是从1开始。

      当小明对所有硬币都进行了一次 Q 操作后,他发现了一个奇迹——所有硬币均为正面朝上。

      小明想知道最开始有多少枚硬币是反面朝上的。于是,他向他的好朋友小M寻求帮助。

      聪明的小M告诉小明,只需要对所有硬币再进行一次Q操作,即可恢复到最开始的状态。然而小明很懒,不愿意照做。于是小明希望你给出他更好的方法。帮他计算出答案。
    输入格式
      输入数据包含一行,两个正整数 n m,含义见题目描述。
    输出格式
      输出一个正整数,表示最开始有多少枚硬币是反面朝上的。
    样例输入
    2 3
    样例输出
    1
    数据规模和约定
      对于10%的数据,n、m <= 10^3;
      对于20%的数据,n、m <= 10^7;
      对于40%的数据,n、m <= 10^15;
      对于10%的数据,n、m <= 10^1000(10的1000次方)。

    题目链接:

      http://lx.lanqiao.cn/problem.page?gpid=T126

    题目大意:

      

    题目思路:

      【高精度】

      高精度即可。

      1 //
      2 //by coolxxx
      3 //
      4 #include<iostream>
      5 #include<algorithm>
      6 #include<string>
      7 #include<iomanip>
      8 #include<memory.h>
      9 #include<time.h>
     10 #include<stdio.h>
     11 #include<stdlib.h>
     12 #include<string.h>
     13 #include<stdbool.h>
     14 #include<math.h>
     15 #define min(a,b) ((a)<(b)?(a):(b))
     16 #define max(a,b) ((a)>(b)?(a):(b))
     17 #define abs(a) ((a)>0?(a):(-(a)))
     18 #define sqr(a) (a)*(a)
     19 #define swap(a,b) (a)^=(b),(b)^=(a),(a)^=(b)
     20 #define eps 1e-8
     21 #define S 10000
     22 #define MAX 2139062143
     23 #define PI 3.1415926535897
     24 #define N 1004
     25 using namespace std;
     26 int n,m,cas,lll;
     27 int px[N],py[N],nn[N],mm[N];
     28 char sn[N],sm[N];
     29 void gjdprint(int a[])
     30 {
     31     int i;
     32     for(i=a[0];i;i--)
     33         printf("%d",a[i]);
     34     puts("");
     35 }
     36 int gjdcompare(int a[],int b[])//a<b->1  a=b->0  a>b->-1
     37 {
     38     int i;
     39     if(a[0]<b[0])return 1;
     40     if(a[0]>b[0])return -1;
     41     for(i=a[0];i;i--)
     42         if(a[i]<b[i])return 1;
     43         else if(a[i]>b[i])return -1;
     44     return 0;
     45 }
     46 void gjdcheng(int a[],int b[],int c[])
     47 {
     48     int i,j,t[N];
     49     memset(t,0,sizeof(t));
     50     t[0]=a[0]+b[0];
     51     for(i=1;i<=a[0];i++)
     52         for(j=1;j<=b[0];j++)
     53             t[i+j-1]+=a[i]*b[j];
     54     for(i=1;i<=t[0];i++)
     55         t[i+1]+=t[i]/10,t[i]%=10;
     56     while(t[t[0]+1])t[0]++;
     57     while(!t[t[0]])t[0]--;
     58     memcpy(c,t,sizeof(t));
     59 }
     60 int calculate(int a[],int b[])
     61 {
     62     int t[N];
     63     memset(t,0,sizeof(t));
     64     gjdcheng(a,a,t);
     65     return gjdcompare(t,b);
     66 }
     67 int main()
     68 {
     69     #ifndef ONLINE_JUDGE
     70     //freopen("1.txt","r",stdin);
     71     //freopen("2.txt","w",stdout);
     72     #endif
     73     int i,j,x,y;
     74     while(~scanf("%s%s",sn,sm))
     75     //while(~scanf("%d",&n) && n)
     76     {
     77         memset(nn,0,sizeof(nn));
     78         memset(mm,0,sizeof(mm));
     79         nn[0]=strlen(sn);
     80         mm[0]=strlen(sm);
     81         for(i=1;i<=nn[0];i++)
     82             nn[i]=sn[nn[0]-i]-'0';
     83         for(i=1;i<=mm[0];i++)
     84             mm[i]=sm[mm[0]-i]-'0';
     85         px[0]=nn[0]/2+1;
     86         py[0]=mm[0]/2+1;
     87         for(i=px[0];i;i--)
     88         {
     89             j=calculate(px,nn);
     90             while(j>=0)
     91             {
     92                 if(j==0)break;
     93                 px[i]++;
     94                 j=calculate(px,nn);
     95             }
     96             if(j==0)break;
     97             px[i]--;
     98         }
     99         for(i=py[0];i;i--)
    100         {
    101             j=calculate(py,mm);
    102             while(j>=0)
    103             {
    104                 if(j==0)break;
    105                 py[i]++;
    106                 j=calculate(py,mm);
    107             }
    108             if(j==0)break;
    109             py[i]--;
    110         }
    111         gjdcheng(px,py,px);
    112         gjdprint(px);
    113     }
    114     return 0;
    115 }
    116 
    117 
    118 /*
    119 //
    120 
    121 //
    122 */
    View Code
  • 相关阅读:
    Learn Goroutine
    Redis eviction policies
    Hungarian Algorithm
    Prime and Factors
    HDU 2642 Stars
    236. Lowest Common Ancestor of a Binary Tree
    Leetcode 96. Unique Binary Search Trees
    Search in Rotated Sorted Array
    ID Generator
    概率问题
  • 原文地址:https://www.cnblogs.com/Coolxxx/p/6671359.html
Copyright © 2011-2022 走看看