1. 程式人生 > >矩陣翻硬幣

矩陣翻硬幣

man 得出 ons 過大 字符 blog 過程 image 什麽

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

如果能理解其內在的含義,可以得40分保命:

技術分享圖片

保命代碼:

技術分享圖片
#include <stdio.h>
#include <memory.h>
#include <math.h>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include 
<map> #define I scanf #define OL puts #define O printf #define F(a,b,c) for(a=b;a<c;a++) #define FF(a,b) for(a=0;a<b;a++) #define FG(a,b) for(a=b-1;a>=0;a--) #define LEN 100 #define MAX 0x06FFFFFF #define V vector<int> using namespace std; typedef long long ll; int main(){ // http://lx.lanqiao.cn/problem.page?gpid=T126
// freopen("D:/CbWorkspace/blue_bridge/矩陣翻硬幣.txt","r",stdin); ll n,m; scanf("%lld%lld",&n,&m); printf("%lld",ll(sqrt(n))*ll(sqrt(m))); return 0; }
View Code

套路理解:

先看 n = 1 的情況:對於(1 , m),只要看它翻轉的次數奇偶就能確定它最終的狀態。因為 x = 1, 每次第一行都要參與翻轉,當 y 能整除 m 的時候,(1 , m)會翻轉,(1 , m)全過程翻轉的次數取決於 m 的約數個數,1 的約數個數為1 , 3 的約數個數為2, 5 的約數個數為2, 9 的約數個數為3。當 m = k^2 (k = 1 ,2 ,3···) 其約數個數為奇數,否則 其約數個數為偶數。 因為一般數約數都是成對出現,而一個數的平方數,有兩個約數相等。
所以,最後(1 , m) m = k^2 (k = 1 ,2 ,3···) 最終狀態為0,其他則為1。 而最後0的個數總和 count = sqrt(m) , 取整。
再來看一般情況:(n , m)最後狀態是什麽?現在行的變化也是它翻轉的因素。從上面容易推出,當m確定後,他的翻轉次數為 n 的約數個數。而(n , m)翻轉的次數 = (n的約數個數 * m的約數個數)。剛才分析了,只有在(n , m)翻轉的次數為奇數時 它的最終狀態為 0。而只有 奇數*奇數 = 奇數,所以n ,m的約數個數必須為奇數,即: n = k^2 (k = 1 ,2 ,3···) 且 m = j^2 (j = 1 ,2 ,3···)。 最後得出結論: 對於n行m列矩陣,經過 Q 操作後 反面的次數 count = sqrt(n) * sqrt(m) ,(取整後再相乘)。 高精度開方: 假設位數為len的整數,開方取整後為一個lenSqrt位數。 當len為偶數,lenSqrt = len / 2 . 當len為奇數,lenSqrt = (len / 2) + 1 . 證明很簡單,這裏就不證了。 現在就簡單了,位數確定了從高位到低位一位一位地確定。比如:sqrt(1028) ,表示對1028開方取整 它開方取整後兩位數.先看第一位: 取 0, 00 * 00 < 1028 所以sqrt(1028) > 00 取 1, 10 * 10 < 1028 所以sqrt(1028) > 10 取 2, 20 * 20 < 1028 所以sqrt(1028) > 20 取 3, 30 * 30 < 1028 所以sqrt(1028) > 30 取 4, 40 * 40 > 1028 所以sqrt(1028) < 40 , 所以第一位取 3 。 第二位: 取 0, 30 * 30 < 1028 所以sqrt(1028) > 30
取 1, 31 * 31 < 1028 所以sqrt(1028) > 31 取 2, 32 * 32 < 1028 所以sqrt(1028) > 32
取 3, 33 * 33 > 1028 所以sqrt(1028) < 33 , 所以sqrt(1028) = 32 。
大數是一樣的道理,只不過大數用字符串保存,字符串相乘也要自己來實現。 結果只得了70分: 技術分享圖片
#include <stdio.h>
#include <memory.h>
#include <math.h>
#include <string>
#include <string.h>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <map>


#define I scanf
#define OL puts
#define O printf
#define F(a,b,c) for(a=b;a<c;a++)
#define FF(a,b) for(a=0;a<b;a++)
#define FG(a,b) for(a=b-1;a>=0;a--)
#define LEN 3000
#define MAX 0x06FFFFFF
#define V vector<int>

using namespace std;

typedef long long ll;

struct hp{
    int len;
    int s[LEN+1];
    hp(){
        len=1;
        int i;
        for(i=1;i<=LEN;i++){
            s[i]=0;
        }
    }
    hp(char* ch){
        int i;
        len=strlen(ch);
        for(i=1;i<=len;i++)
            s[i]=ch[len-i]-48;
        for(;i<=LEN;i++)
            s[i]=0;
    }
    void print(){
        int i;
        for(i=len;i>=1;i--)
            printf("%d",s[i]);
    }
    string output(){
        int i;
        string ans;
        for(i=len;i>=1;i--){ 
            char buf[100];
            sprintf(buf,"%d",s[i]);
            ans+=buf;
        }
        return ans;
    } 
};

int compare(const hp& a,const hp& b){
    int len=max(a.len,b.len);
    while(len>0 && a.s[len]==b.s[len]) len--;
    if(len==0)
        return 0;
    else return a.s[len]-b.s[len];
}

void multiplyh(const hp& a,const hp& b,hp& c){
    int i,j,len=a.len+b.len+1;
    c=hp();
    for(i=1;i<=a.len;i++){
        for(j=1;j<=b.len;j++){
            c.s[i+j-1]+=a.s[i]*b.s[j];
            c.s[i+j]+=c.s[i+j-1]/10;
            c.s[i+j-1]%=10;
        }
    }
    while(len>1 && c.s[len]==0) len--;
    c.len=len;
}

void square(const hp&a,hp &c){
    multiplyh(a,a,c);
}

void sqrth(const hp&a,hp &c){
    int i,j,len=(a.len+1)/2;
    c=hp();
    hp t;
    c.len=len;
    for(i=len;i>=0;i--){
        for(j=0;j<=9;j++){
            c.s[i]=j;
            square(c,t);
            if(compare(t,a)>0){
                c.s[i]=j-1;
                break;
            }
        }
    }
}

int main(){
//    http://lx.lanqiao.cn/problem.page?gpid=T126
//    freopen("D:/CbWorkspace/blue_bridge/矩陣翻硬幣.txt","r",stdin);
    char buf[LEN];
    scanf("%s",buf);
    hp a(buf);
    scanf("%s",buf);
    hp b(buf);
    hp c,d,e;
    sqrth(a,c);
    sqrth(b,d);
    multiplyh(c,d,e);
    e.print();
    return 0;
}
View Code

矩陣翻硬幣