解题报告 Function

Function

Description

我们定义如下函数:,给出x的初始值x0,通过不断地代入x可以求得新的x'、x'',通过x'和x''代入公式又可以得到新的4个值。

给出a1,b1,d1,a2,b2,d2,初始值x0,问你迭代多次后得到的第n小的值是多少。

Input Format

第一行两个数,为x0和n,第二行为a1,b1,d1,第三行为a2,b2,d2

Output Format

一个正整数,表示第n小的数的值。

Sample Input

3 10

4 3 3

17 8 2

Sample Output

65

【Hint】

    1、c=3

2、F1(3)=4×3/3+3    :7

3、F1(7)=4×7/3+3    :12

4、F1(12)=4×12/3+3  :19

5、F1(19)=4×19/3+3  :28

6、F2(3)=17×3/2+8   :33

7、F1(28)=4×28/3+3  :40

8、F1(33)=4×33/3+3  :47

9、F1(40)=4×40/3+3  :56

10、F1(47)=4×47/3+3 :65

Data

对于100%的数据

X0≤100N≤4000000

a1b1d1a2b2d2≤20

涉及到的所有数字都将是整数。

 

 

这是一种经典的解题方法,首先设置两个指针,分别指向 x0 经过函数 1 和 函数 2 所得出来的值,然后,从这两个值中选一个比较小的,然后扩展出两个值,入队,然后,将这两个值中较小的那个指针加一。如果相同的话,两个指针都加一。这样依次类推,当总共入队的值的数量达到 n 的时候,输出。

 

注意一定要开一个队列,开两个队列不好判重,话说我就是 WA 在这了。

 

Program _Function(input,output);

var

  q:array[0..4000000] of int64;

  n,p1,p2,tail,a1,a2,b1,b2,d1,d2:longint;

  c,c1,c2:int64;

begin

  assign(input,'Function.in'); reset(input);

  assign(output,'Function.out'); rewrite(output);

  readln(c,n);

  readln(a1,b1,d1,a2,b2,d2);

  tail:=1;

  q[1]:=c;

  p1:=1;

  p2:=1;

  while tail<n do

    begin

      c1:=q[p1]*a1 div d1+b1;

      c2:=q[p2]*a2 div d2+b2;

      if (c1<c2)and(c1>q[tail]) then

        begin

          inc(tail);

          inc(p1);

          q[tail]:=c1;

        end else

      if (c2<c1)and(c2>q[tail]) then

        begin

          inc(tail);

          inc(p2);

          q[tail]:=c2;

        end else

      if (c1=c2)and(c1>q[tail]) then

        begin

          inc(tail);

          q[tail]:=c1;

          inc(c1);

          inc(c2);

        end else

      if c1<c2 then inc(p1) else

      if c1>c2 then inc(p2) else

        begin

          inc(p1);

          inc(p2);

        end;

    end;

  writeln(q[n]);

  close(input); close(output);

end.

posted @ 2011-10-29 15:16  木小漾  阅读(272)  评论(0编辑  收藏  举报