1. 程式人生 > >codeforces 894B. Ralph And His Magic Field (數學題+思維)

codeforces 894B. Ralph And His Magic Field (數學題+思維)

題目大意

在一個 n x m 的矩陣中插入任意數字,使得每一行每一列數的乘積為 k,其中 k要麼是 1 要麼是 -1. 我們注意到插入的數只可能是 1 或 -1.

思路

我們只考慮 -1 的數目。當 k=-1 時,每行每列有奇數個 -1,其他的為 1;當 k=1 時,每行每列有偶數個 -1,其他的為 1. 但是你添入一個數會同時影響到一行和一列,又因為資料範圍比較大,直接模擬肯定是行不通的,應該存在某個公式。

換種思維考慮,現在把最後一行和最後一列空出來,其他的空無論添任何值,我們都可以通過在最後一行和最後一列新增 1 或 -1 使得滿足題意,因為這樣我們可以控制 -1 的奇偶。所以有 (n-1)*(m-1) 個值是可以任添的,每個空有 2 種選擇,所以結果是 2^(n-1)*(m-1)

,直接做會超時,可以用快速冪取模。

注意,上面說的不完全對,是有特殊情況存在的,當 k=-1 時,如果行列的奇偶性質不同,則怎麼填都會有衝突,所以這個時候答案是 0,需要特判。

程式碼

#include<stdio.h>
#include<string.h>
typedef long long LL;

LL mod=1e9+7;

LL pow(LL a,LL b)
{ //快速冪取模 
	LL r=1,base=a;
	while(b)
	{
		if(b&1) r=r*base%mod;
		base=base*base%mod;
		b>>=1;
	}
	return r;
}

int main()
{
	int k;
	LL n,m,ans;
	while(~scanf("%lld%lld%d",&n,&m,&k))
	{
		if(n%2!=m%2 && k==-1) printf("0\n"); //特判 
		else 
		{ //答案為 2^(n-1)*(m-1)
			ans=pow(pow(2,n-1),m-1);
			printf("%lld\n",ans);
		}
	}
	return 0;
}

相關推薦

codeforces 894B. Ralph And His Magic Field 數學題+思維

題目大意: 在一個 n x m 的矩陣中插入任意數字,使得每一行每一列數的乘積為 k,其中 k要麼是 1 要麼是 -1. 我們注意到插入的數只可能是 1 或 -1. 思路: 我們只考慮 -1

Codeforces 894 B Ralph And His Magic Field

con gic ans into dex style cond code pan Discription Ralph has a magic field which is divided into n?×?m blocks. That is to say, there ar

Codeforces 894 B. Ralph And His Magic Field

Ralph has a magic field which is divided inton × mblocks. That is to say, there arenrows andmcolumns on the field. Ralph can put an integer in each bloc

Codeforces Round #447 (Div. 2) B. Ralph And His Magic Field

Ralph has a magic field which is divided into n × m blocks. That is to say, there are n rows and m columns on the field. Ralph can put an integer in eac

Codeforces894B-Ralph And His Magic Field

/** 很有意思的一道題 題意: n * m 的矩陣, 要求 每一行每一列 的 乘積都為 k (k 為 1 or -1) 問有多少種情況 注意 fst!如果 n 和 m 的奇偶性

CodeForces - 305C Ivan and Powers of Two數學+思維

CodeForces - 305C Ivan and Powers of Two(數學+思維) Ivan has got an array of n non-negative integers a1, a2, …, an. Ivan knows that the array is sor

codeforces 894D Ralph And His Tour in Binary Country

D. Ralph And His Tour in Binary Country time limit per test 2.5 seconds memory limit per test 512 megabytes input standard input output

Codeforces 514C. Watto and Mechanism解題報告字典樹

test names cin 單詞 turn void can include 傳送門 傳送門 題意:給你一個字典和一些詢問,問你對於每個詢問的字符串必須更改一個字符,能否得到字典中的某一個單詞。 思路:先構造一顆字典樹,然後搜一遍就行了,要註意strlen不能每次都用,常

Codeforces Round #508 (Div. 2).C. Gambling模擬+思維

C. Gambling time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Two pla

[Codeforces 1042E]Vasya and Magic Matrix期望 DP

Address 洛谷 RemoteJudge Codeforces 1042E Meaning 一個 n

CodeForces - 894E Ralph and Mushrooms (強連通縮點+dp)

push dde using scanf print freopen eof sqrt col 題意:一張有向圖,每條邊上都有wi個蘑菇,第i次經過這條邊能夠采到w-(i-1)*i/2個蘑菇,直到它為0。問最多能在這張圖上采多少個蘑菇。 分析:在一個強連通分量內,邊可以無限

CodeForces - 276E Little Girl and Problem on Trees線段樹

題意 現在給你一棵這樣的樹,除了根節點外,其他的所有節點度都為2,也就是說除了根節點之外其他的節點都只能有一個節點,現在有兩種操作: 1。以當前節點為中心,距離為d的範圍內的點全部都加上x。 2。查詢某個點的值 思路* 觀察這樣一張圖,你會發現他們其實都是一條一條的鏈狀的,所以對於更新

codeforces 894E Ralph and Mushrooms 強連通dp

E. Ralph and Mushrooms time limit per test 2.5 seconds memory limit per test 512 megabytes input standard input output standard ou

Codeforces Round #512 (Div. 2) D. Vasya and Triangle幾何+思維

  題目   題意:   給出 n,m,k ,讓你在長為 n,寬為 m 的座標系裡構建一個三角形,使得面積= n*m/k。如果存在,輸出“YES”,輸出三角形三個頂點的座標;  如果不存在,輸出“NO”。   思路:   參考其他人部落格。 設長為a ,寬

Mike and distribution CodeForces - 798D 貪心+思維

題目連結 TAG: 這是我近期做過最棒的一道貪心思維題,不容易想到,想到就出乎意料。 題意:給定兩個含有N個正整數的陣列a和b,讓你輸出一個數字k ,要求k不大於n/2+1,並且輸出k個整數,範圍為1~n的不重複數字, 要求這k個數字為下標的對應a和b中的數的和乘以2的值  分別大於a和b 的

[Codeforces 711E] ZS and The Birthday Paradox 數學+Legendre公式

Codeforces - 711E 生日攻擊的模型,現在有 2^n天,k個人,問不衝突的概率 其中 n和 k都是在 LL範圍內的數,並且要輸出既約分數 很容易就列出計算式,難點就在

ZOJ - 4082:Little Sub and his Geometry Problem 雙指針

span it is pre plan sep std lov next def Little Sub loves math very much, and has just come up with an interesting problem when he is wor

CodeForces 518E Arthur and Questions貪心 + 思維題解

ios else scanf style cor div rect 數字 \n 題意:給你a1~an,k,要求a1 + ... + ak < a2 + .... + ak+1 < a3 + ... + ak+2 <...,然後這裏的ai有可能是?,要求你填

codeforces 701 D. As Fast As Possible數學題

1.0 我們 force 時間 tar 遠的 每次 space http 題目鏈接:http://codeforces.com/problemset/problem/701/D 題意:給你n個人,每個人走路的速度v1,有一輛車速度為v2,每次可以載k個人,總路程為l,每個

codeforces 894C - Marco and GCD Sequence - [有關gcd數學題]

mes when section got 操作 positive ive cin style 題目鏈接:https://cn.vjudge.net/problem/CodeForces-894C In a dream Marco met an elderly man wit