Codevs P1654方程的解

来源:互联网 发布:从零开始学java 编辑:程序博客网 时间:2024/05/16 23:40

Codevs P1654方程的解


题目描述 Description

佳佳碰到了一个难题,请你来帮忙解决。对于不定方程a1+a2+… +ak-1 +ak=g(x),其中k≥2且k ∈ N*,x是正整数,g(x) =xx mod 1000(即xx除以1000的余数),x,k是给定的数。我们要求的是这个不定方程的正整数解组数。举例来说,当k=3, x=2时,分别为(a1,a2,a3)=(2,1,1),(1,2,1),(1,1,2)。


输入输出


输入描述 Input Description

输人只有一行,为用空格隔开的两个正整数,依次为k,x。

输出描述 Output Description

输出只有一行,为方程的正整数解组数。

样例 Sample


样例输入 Sample Input

3 2

样例输出 Sample Output

3

数据范围及提示 Data Size & Hint

【数据范围】
对于40%的数据,ans ≤ 1016;
对于100%的数据,k≤ 100,x≤231一1,k ≤g (x)。

分析

因为要求正整数解,那么显然就是讲g[x]拆成g[x]个1,利用隔板法求得结果即c(g[x]-1,k-1);
快速幂求x^x mod 1000;
然后高精乘单精,高精除以单精即可

代码如下

program p1654;type num=array[0..10000] of longint;var k,x:int64;    i:longint;    f,ans:num;function power(a,b:int64):longint;var sum:int64;begin sum:=1; a:=a mod 1000; while b<>0 do  begin   if b and 1=1 then sum:=(sum*a) mod 1000;   b:=b>>1;   a:=(a*a) mod 1000;  end; exit(sum);end;function mutiply(x:longint):num;var i:longint;begin for i:=1 to f[0] do  f[i]:=f[i]*x; for i:=1 to f[0] do  begin   f[i+1]:=f[i+1]+f[i] div 10;   f[i]:=f[i] mod 10;  end; while f[f[0]+1]>0 do  begin   inc(f[0]);   f[f[0]+1]:=f[f[0]+1]+f[f[0]] div 10;   f[f[0]]:=f[f[0]] mod 10;  end; exit(f);end;function subtract(x:longint):num;var rest,i:longint;begin fillchar(subtract,sizeof(subtract),0); rest:=0; for i:=f[0] downto 1 do  begin   rest:=rest*10+f[i];   subtract[i]:=rest div x;   rest:=rest mod x;  end; subtract[0]:=f[0]; while (subtract[0]>0) and (subtract[subtract[0]]=0) do dec(subtract[0]); exit(subtract);end;function c(n,m:longint):num;var i:longint;begin f[0]:=1; f[1]:=1; for i:=n-m+1 to n do  f:=mutiply(i); for i:=2 to m do  f:=subtract(i); exit(f);end;begin readln(k,x); x:=power(x,x); ans:=c(x-1,k-1); for i:=ans[0] downto 1 do  write(ans[i]);end.

评测结果

运行结果
测试点#equation1.in 结果:AC 内存使用量: 364kB 时间使用量: 1ms
测试点#equation10.in 结果:AC 内存使用量: 368kB 时间使用量: 1ms
测试点#equation2.in 结果:AC 内存使用量: 364kB 时间使用量: 1ms
测试点#equation3.in 结果:AC 内存使用量: 368kB 时间使用量: 1ms
测试点#equation4.in 结果:AC 内存使用量: 368kB 时间使用量: 1ms
测试点#equation5.in 结果:AC 内存使用量: 368kB 时间使用量: 3ms
测试点#equation6.in 结果:AC 内存使用量: 368kB 时间使用量: 1ms
测试点#equation7.in 结果:AC 内存使用量: 364kB 时间使用量: 3ms
测试点#equation8.in 结果:AC 内存使用量: 364kB 时间使用量: 2ms
测试点#equation9.in 结果:AC 内存使用量: 368kB 时间使用量: 2ms

#


这里写图片描述


这里写图片描述

0 0