数论+快速幂 SRM 661 Div1 450: ColorfulLineGraphs
来源:互联网 发布:highcharts 更新数据 编辑:程序博客网 时间:2024/05/21 17:34
http://community.topcoder.com/stat?c=problem_statement&pm=13765
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 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
- ClassColorfulLineGraphs
- MethodcountWays
- Parameterslong long , long long , int
- Returnsint
- Method signatureint countWays(long long N, long long K, int M)
Limits
- Time limit (s)2.000
- Memory 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.
Test cases
- N3
- K2
- M100000
Returns24- N15
- K3
- M1000000
Returns510625- N100000
- K100000
- M999999
Returns185185- N1000000000000
- K6
- M1000000
Returns109376- N5000
- K1000000000000
- M314159
- Returns202996
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 题解
The constraints, except for
M are much larger than in the division 2 version. It is interestingM remains relatively small, as ifO(M) or similar was intended. The large constraints forN andK suggest we might need to get creative, however.Imagine we are deciding what to do for the nodes in order from left to right (lowest index to highest). Imagine we already made decisions for the first
i nodes. We know their colors and we know which edges (or lack of) we picked for each of them. Remember (from division 2) that when assigning things to do with the higher nodes, we only need to remember the number of nodes of each color that have been assigned, we don't need to know the edges. Let's count the number of decisions for node #i :- We may decide to use color #0. Then we have to pick an edges. We can pick no edge at all or we can connect node
i to any node of a color different than 0. in which case we have1+(i−count(i,0)) options, wherecount(i,0) is the number of nodes of color 0 among the firsti nodes. - For color 1:
1+i−count(i,1) . - ...
- For color +
k−1 :1+i−count(i,k−1) .
What happens if we add together all the options?
1+i−count(i,0) +1+i−count(i,1) +1+i−count(i,2) +... +1+i−count(i,k−1) k times 1,k timesi and the sum of all counts:k+ki−∑j=0j<k(count(i,j)) This is the interesting part: What is
∑j=0j<k(count(i,j)) ? The sum of all the counts of colors among the firsti nodes. This will be always equal toi . There arei nodes in total and the sum of all counts for each color will always bei . This means that the number of options for nodei is:k+ki−i=k+i(k−1) . So in fact, the color choices beforei are not relevant for the final result, only the total number of available colors. This way we can have anO(N) formula, just multiplyk+i(k−1) for eachi :∏i=0i<N(i(k−1)+k) N is still too large even for anO(N) so we need more improvements. This is where the small constraints onM can come into play. Fromi=0 , toN−1 , the valueimodM behaves in a cycle. So imagineM=3 . Fori=0,1,2,3,4,5,6,7 , the values ofimod3 will be:0,1,2,0,1,2,0,1 . Remember that(a⋅b)modM=((amodM)(bmodM))modM , so∏i=0i<N(i(k−1)+k)modM is equal to:∏i=0i<N((i(k−1)+k)modM) . Each(i(k−1)+k)modM depends solely oni , more specificallyimodM :(i(k−1)+k)modM≡(((imodM)(k−1)modM)+(kmodM)) The only non-constant part is
imodM . This part is cyclic and repeats everyM parts. So back to theN=7,M=3 example:i=0,i=3,i=6 all will yield the same value of(i(k−1)+k)modM . We can say the same abouti=1,i=4 andi=2,i=5 . We just need to use this cycle to our advantage.So for simplicity, let's name
g(i)=(i(k−1)+k)modM . We need to calculate:(g(0)⋅g(1)⋅g(2)⋅...⋅g(N−1))modM We have the knowledge that for every pair
i≡jmodM ,g(i)≡g(j)modM (The values ofg(i) are repeated everyM times). So imagine this:(g(0)⋅g(1)⋅g(2)⋅...⋅g(M−1)⋅g(M)⋅g(M+1)⋅g(M+2)⋅...⋅g(2M−1)⋅...g((N/M)−1) ⋅g((N/M)⋅M)⋅g((N/M)⋅M+1)⋅...⋅g((N/M)⋅M+(NmodM)−1)modM What we have here is the same
M values are repeated over and over again, the firstM values, the nextM values and so and so. They will be repeated(N/M) times (integer, round down division). There will be some extra values depending ifM dividesN evenly.NmodM additional values (The remainder).(g(0)⋅g(1)⋅g(2)⋅...⋅g(M−1)⋅g(0)⋅g(1)⋅g(2)⋅...⋅g(M−1)⋅...g(0)⋅g(1)⋅g(2)⋅...⋅g(M−1) ⋅g(0)⋅g(1)⋅...⋅g((NmodM)−1)modM The product
g(0)⋅g(1)⋅g(2)⋅...⋅g(M−1) is repeated exactlyN/M times. So we can actually calculate the product of the first(N/M)M values by using the following:(g(0)⋅g(1)⋅g(2)⋅...⋅g(M−1))N/M This requires
O(M) time to calculate the product of the firstM values, then we can useO(log(N/M)) time to calculate the power (for example, using exponentiation by squaring).Finally we need to multiply that to:
g(0)⋅g(1)⋅...⋅g((NmodM)−1) , This requiresO(M) further steps. In total we have anO(O(log(N/M))+M) algorithm:long mod_pow(long x, long y, long M){ //exponentiation by squaring: long r = 1; long a = x; while (y > 0) { if (y % 2 == 1) { r = (r * a) % M; } a = (a * a) % M; y /= 2; } return r;} int countWays(long N, long K, int M){ long p = 1, q = 1; // p will store product of all i < M // q will store product of all i &ly; N%M for (int i = 0; i < M; i++) { long x = (i*(K - 1) + K) % M; p = (p * x) % M; if (i < N%M) { q = p; } } return (mod_pow(p, N/M, M) * q) % M;}
看完有没有很爽的感觉^-^- We may decide to use color #0. Then we have to pick an edges. We can pick no edge at all or we can connect node
- 数论+快速幂 SRM 661 Div1 450: ColorfulLineGraphs
- SRM 450 div1(practice)
- SRM 606 DIV1 450
- TC SRM 552 DIV1 100PT(数论)
- Topcoder SRM661 ColorfulLineGraphs,数学组合问题+快速幂求解
- 最小公倍数 SRM 661 Div1 250: MissingLCM
- Topcoder SRM 661 Div1 Easy: MissingLCM
- SRM 465(DIV1 DIV2)
- SRM 465(DIV1 DIV2)
- SRM 144 DIV1 second
- SRM 507 DIV1 B
- SRM 513 DIV1 C
- SRM 500 DIV1 B
- 1-SRM 144 DIV1
- TC SRM 559 DIV1
- SRM 195 Div1 总结
- SRM 497 DIV1 550
- SRM 449 div1 (practice)
- php 操作mongodb
- ceil、round与floor
- 清除浮动的方法
- ROS学习--(十二)编写一个简单的发布器(publisher)
- labview2009以上的版本将程序保存成png图片格式
- 数论+快速幂 SRM 661 Div1 450: ColorfulLineGraphs
- SVN的概念及SVN服务器端的安装和配置
- PAT 1076. Forwards on Weibo (30)
- Android低端设备加载大图片、优化设备加载图片
- Android实战简易教程-第三十枪(实例解析Application的用法)
- centos7 iptables的安装和配置
- Python的机器学习库汇总与梳理
- 循环链表
- 使用SharedPerference保存数据
The 24 different graphs are shown below. In each picture, the vertices have labels 1, 2, 3 from the left to the right.