51Nod 1024 矩阵中的不重复元素

传送门

Tags:数学 矩阵 set

题意:一个n行m列的矩阵,第一个元素是a^b。每列a+1,每行b+1,形式都是(a)^(b),问矩阵中不重复元素的数量

解析:如果暴力,首先存不下100^100那么大的数。其次,极限数据会超时。而选择用对数,就可以轻松解决这两个问题,因为最终只是求不重复元素的个数,这个用set就可以搞定,那么只要我们能建立一个由原来的数到其他数的映射就可以等效地统计出来。即 log2(a^b) = b * log2(a) ;

代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <iomanip>
#include <bitset>
using namespace std;

#define eps         (1e-6)
#define LL          long long
#define pi          acos(-1.0)
#define ALL(a)      (a.begin(),(a.end())
#define ZERO(a)     memset(a,0,sizeof(a))
#define MINUS(a)    memset(a,0xff,sizeof(a))
#define IOS         cin.tie(0) , cout.sync_with_stdio(0)
#define PRINT(a,b)  cout << "#" << (a) << " " << (b) << endl
#define DEBUG(a,b)  cout << "$" << (a) << " " << (b) << endl
#define line        cout << "\n--------------------\n"

const LL INF = 1e18+100;
const int MAXN = 1e6;

int m,n,a,b;

void SOLVE()
{
    set<double> s;
    double temp;
    for(int i=a; i<a+n; ++i)
        for(int j=b; j<b+m; ++j)
        {
            temp = j*log2(i);
            s.insert(temp);
        }
    cout << s.size();
    return;
}

int main()
{
    IOS;
    cin >> m >> n >> a >> b;
    SOLVE();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40526226/article/details/85241707