Topcoder SRM661 ColorfulLineGraphs,数学组合问题+快速幂求解

Problem Statement

 

Bob is going to create a graph with N nodes. The graph will be constructed in two steps. First, Bob will take N isolated vertices, label them 1 through N and color each of them using one of K colors. Then, Bob will add some directed edges to the graph. For each i between 2 and N, inclusive, Bob may choose a single value j < i such that the nodes i and j have different colors. If he does, he will add the edge from i to j to his graph. Note that Bob may choose not to add any edge from node i, even if there are some valid choices of j.

Two graphs are considered the same if they have the same node colors and the same set of edges.

You are given the long longs N and K. You are also given an int M. Compute and return the number of different graphs Bob may construct, modulo M.

Definition

 
Class:ColorfulLineGraphs
Method:countWays
Parameters:long long, long long, int
Returns:int
Method signature:int countWays(long long N, long long K, int M)
(be sure your method is public)

Limits

 
Time limit (s):2.000
Memory limit (MB):256
Stack limit (MB):256

Constraints

-N will be between 1 and 1,000,000,000,000 (10^12), inclusive.
-K will be between 1 and 1,000,000,000,000 (10^12), inclusive.
-M will be between 2 and 1,000,000 (10^6), inclusive.

Examples

0) 
 
3
2
100000
Returns: 24

The 24 different graphs are shown below. In each picture, the vertices have labels 1, 2, 3 from the left to the right.




只需要在不考虑模数的情况下,推出公式:

再考虑模数的情况,借住快速幂和模的特性来求出答案。


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值