CRCLE_UI - Colorful Circle (EASY)

------------------------
I take this problem from my midterm exam today, because for me and some of my friends it's interesting, so I decided to translated this problem into English and upload this problem to SPOJ. See the original problem in Indonesian language here.
------------------------ 

Given N sectors where 1<N<101000, from a circle that sown in the picture below:

circle

We will color each sector with K different colors, where 2<K<101000 such that each sector colored with one color and each adjacent sector must have different color. Your task is to count how many ways to color all that sectors.

Input

First line, there is a number T(0<T<1000) denoting number of test cases, then T lines follow.
each line containing two integers: N and K separated by a space.

Output

For each test case, output number of ways to color the circle, since the number can be too large, take modulo 109+7.

Example

Input:
2
2 3
3 3

Output:
6
6

Explanation:

For the first case, we have two sectors and three colors, here is all possibilities:

2 sectors 3 colors

For second test case, we have three sector and three colors, here is all possibilities:

3 sectors 3 colors

 

Time limit set so that ~128 Bytes of python 3 code can get accepted, also my C top speed program AC in 0.12s
Have fun :) 

 

See also: Another problem added by Tjandra Satria Gunawan


Added by:Tjandra Satria Gunawan
Date:2012-11-14
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:Discrete Math II, Midterm exam, question number 4, University of Indonesia, 14 November 2012 | Translated by: Tjandra

hide comments
2018-07-06 14:25:42
Good problem.
2017-07-13 08:15:09 hanstan
Nailed it 0.00s :)
2015-01-23 05:45:42 Itachi_Uchiha
Tjandra , some more test cases please
for hugh numbers
2013-07-01 21:20:37 Viplov Jain
@Tjandra: please look at my submission,why WA? ,submission id->9578731
never mind, i got it ,was using the wrong formula
got AC :)

Last edit: 2013-07-07 02:41:42
2013-06-05 18:42:04 Yashpal
@Tjandra:if i hav to calculate the no. of ways to color the n sectors with all the k -colors or taken some colors at a time.....i.e if k<=n or not....
2013-05-27 08:55:53 maradona
@Tjandra my id is 9353125
ًWhy slow ???
<u><b>Ans</b></u>: Your formula isn't fast enough.

<u><b>Edit</b></u>: It's really along time but i like this :(.
Another thing spoj is very disappointed for java programmers.

Last edit: 2014-12-31 21:30:44
2013-04-06 08:56:32 abdou_93
thanks @Tjandra for Reply ... but still WA .... I think i can not AC this problem now :(

finally solved :D..

Last edit: 2013-05-23 13:25:10
2013-04-04 11:20:23 abdou_93
please @Tjandra ..can you look at my submission (id->9035872)...
thanks what ever :D
Ans: You getting WA because in your program output there are many negative value.

Last edit: 2013-04-06 00:29:06
2013-01-16 10:28:01 Avinash Thummala
@Tjandra If possible please have a look at my submission (id->8509987). Quite new to Python, but used it anyway as I thought that Montgomery reduction would be useful.
Not really sure how I can optimize even further. Would definitely appreciate a hint.
Ans: Sorry but formula that you've used is incorrect... And to speed up your code try to reduce mod operations...

Last edit: 2013-01-19 11:26:00
2013-01-01 03:21:24 YangYue
I've also thougth about this problem, so
it's easy for me :-)
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.