hdu 5995 Kblack loves flag


Problem Description
Kblack loves flags, so he has infinite flags in his pocket.

One day, Kblack is given an n∗m chessboard and he decides to plant flags on the chessboard where the position of each flag is described as a coordinate (x,y), which means that the flag is planted at the xth line of the yth row.

After planting the flags, Kblack feels sorry for those lines and rows that have no flags planted on, so he would like to know that how many lines and rows there are that have no flags planted on.

Well, Kblack, unlike you, has a date tonight, so he leaves the problem to you. please resolve the problem for him.


Input
You should generate the input data in your programme.

We have a private variable x in the generation,which equals to seed initially.When you call for a random number ranged from [l,r],the generation will trans x into (50268147x+6082187) mod 100000007.And then,it will return x mod (r−l+1)+l.

The first line contains a single integer T refers to the number of testcases.

For each testcase,there is a single line contains 4 integers n,m,k,seed.

Then,you need to generate the k flags' coordinates.

For i=1⋯k,firstly generate a random number in the range of [1,n].Then generate a random number in the range of [1,m].

You can also copy the following code and run "Init" to generate the x[],y[] (only for C++ players).

<pre>
const int _K=50268147,_B=6082187,_P=100000007;
int _X;
inline int get_rand(int _l,int _r){
_X=((long long)_K*_X+_B)%_P;
return _X%(_r-_l+1)+_l;
}
int n,m,k,seed;
int x[1000001],y[1000001];
void Init(){
scanf("%d%d%d%d",&n,&m,&k,&seed);
_X=seed;
for (int i=1;i<=k;++i)
x[i]=get_rand(1,n),
y[i]=get_rand(1,m);
}
</pre>

(1≤T≤7),(1≤n,m≤1000000),(0≤k≤1000000),(0≤seed<100000007)


Output
For each testcase,print a single line contained two integers,which respectively represent the number of lines and rows that have no flags planted.


Sample Input

2
4 2 3 233
3 4 4 2333



Sample Output

2 1

1 0


翻译:

kblack喜欢旗帜(flag),他的口袋里有无穷无尽的旗帜。

某天,kblack得到了一个n∗mn*mnm的方格棋盘,他决定把kkk面旗帜插到棋盘上。

每面旗帜的位置都由一个整数对(x,y)\left(x,y \right)(x,y)来描述,表示该旗帜被插在了第xxx行第yyy列。

插完旗帜后,kblack突然对那些没有插过旗帜的行和列很不满,于是他想知道,有多少行、列上所有格子都没有被插过旗帜。

kblack还要把妹,于是就把这个问题丢给了你,请你帮他解决。

由于本题输入数据较大,所以采取在程序内生成数据的方式。

随机数产生器中有个内部变量xxx初始时为seedseedseedseedseedseed是我们提供的随机种子。每次请求生成一个[l,r]\left[l,r \right][l,r]内的随机数时,它会将xxx变为(50268147x+6082187) mod 100000007\left(50268147x+6082187\right)\ mod\ 100000007(50268147x+6082187) mod 100000007,然后返回x mod (r−l+1)+lx\ mod\ \left(r-l+1 \right)+lx mod (rl+1)+l。

输入包含多组数据。第一行有一个整数TTT,表示测试数据的组数,对于每组数据:

输入一行3个整数nnnmmmkkkseedseedseed分别表示棋盘的行数、列数、棋盘上旗帜的面数、随机种子。

接下来,你需要按顺序生成k面旗帜的位置信息。

对于每面旗帜,依次生成一个[1,n]\left[1,n \right][1,n]内的随机数和一个[1,m]\left[1,m \right][1,m]内的随机数,分别表示xxxyyy。

如果你无法理解数据生成的过程,你可以复制以下代码并调用Init函数来生成数据(限C++选手)。

`
const int _K=50268147,_B=6082187,_P=100000007;
int _X;
inline int get_rand(int _l,int _r){
	_X=((long long)_K*_X+_B)%_P;
	return _X%(_r-_l+1)+_l;
}
int n,m,k,seed;
int x[1000006],y[1000006];
void Init(){
	scanf("%d%d%d%d",&n,&m,&k,&seed);
	_X=seed;
	for (int i=1;i<=k;++i)
		x[i]=get_rand(1,n),
		y[i]=get_rand(1,m);
}

思路:

用两个布尔数组分别维护每个行/列是否被插过旗帜,最后枚举每一行、列统计答案即可。空间复杂度O(n+m)O(n+m)O(n+m),时间复杂度O(n+m+k)O(n+m+k)O(n+m+k)。  生成随机数 并统计即可。


AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
int x[1000006],y[1000006];
int vis1[1000006],vis2[1000006];
const int _K=50268147,_B=6082187,_P=100000007;
int _X;
int n,m,k,seed,t;
inline int get_rand(int _l,int _r)
{
    _X=((long long)_K*_X+_B)%_P;
    return _X%(_r-_l+1)+_l;
}
void Init()
{
    for(int i = 1; i <= k; i++)
    {
        x[i] = get_rand(1, n);
        vis1[x[i]] = 1;
        y[i] = get_rand(1, m);
        vis2[y[i]] = 1;
    }
}

int main()
{
    cin >> t;
    while(t--)
    {
        cin >> n >> m >> k >> seed;
        memset(vis1, 0, sizeof(vis1));
        memset(vis2, 0, sizeof(vis2));
        _X = seed;
        Init();
        int sumx = 0, sumy = 0;
        for(int i = 1; i <= n; i++)
        {
            if(!vis1[i])
                sumx++;
        }
        for(int i = 1; i <= m; i++)
        {
            if(!vis2[i])
                sumy++;
        }
        cout << sumx << " " << sumy << endl;
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值