方程的解(equation.pas/c/cpp)
【问题描述】
佳佳碰到了一个难题,请你来帮忙解决。
对于不定方程a1+a2+…+ak-1+ak=g(x),其中k≥2且k∈N,x是正整数,g(x)=xxmod 1000(即xx除以1000的余数),x,k是给定的数。我们要求的是这个不定方程的正整数解组数。
举例来说,当k=3,x=2时,分别为(a1,a2,a3)=(2,1,1)'(1,2,1),(1,1,2)。
【文件输入】
输入文件equation.in有且只有一行,为用空格隔开的两个正整数,依次为k,x。
【文件输出】
输出文件equation.out有且只有一行,为方程的正整数解组数。
【样例输入】
3 2
【样例输出】
3
【数据范围】
对于40%的数据,ans≤1016;对于100%的数据,k≤100,x≤231-1,k≤g(x)。
=============
先一个快速幂...
再一个组合数...隔板法..
==============================================
{
ID:jie19952
PROG:
LANG:PASCAL
}
var
k,x:longint;
value:longint;
shu:array[0..2569]of longint;
procedure init;
begin
assign(input,'equation.in');
assign(output,'equation.out');
reset(input); rewrite(output);
end;
procedure terminate;
begin
close(input); close(output);
halt;
end;
function get(a,b:longint;c:longint):longint;
var
t,y:int64;
begin
t:=1; y:=a;
while b<>0 do
begin
if (b and 1)=1 then
t:=t*y mod c;
y:=y*y mod c;
b:=b shr 1;
end;
exit(t);
end;
procedure main;
var
i,j:longint;
begin
readln(k,x);
value:=get(x,x,1000);
fillchar(shu,sizeof(shu),0);
shu[1]:=1; shu[0]:=1;
value:=value-1;
for i:=k to value do
begin
for j:=1 to shu[0] do
shu[j]:=shu[j]*i;
j:=1;
while (shu[j]<>0)or(j<=shu[0]) do
begin
shu[j+1]:=shu[j+1]+shu[j] div 10;
shu[j]:=shu[j] mod 10;
inc(j);
end;
shu[0]:=j-1;
end;
for i:=2 to value-k+1 do
begin
//chufa;
for j:=shu[0] downto 2 do
begin
shu[j-1]:=(shu[j] mod i)*10+shu[j-1];
shu[j]:=shu[j] div i;
end;
shu[1]:=shu[1] div i;
while shu[shu[0]]=0 do
begin
dec(shu[0]);
end;
end;
for i:=shu[0] downto 1 do
write(shu[i]);
end;
begin
init;
main;
terminate;
end.