您的位置:首页 > 其它

A - A Simple Math Problem

2015-07-20 17:26 239 查看
Description

Lele now is thinking about a simple function f(x).

If x < 10 f(x) = x.
If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10);
And ai(0<=i<=9) can only be 0 or 1 .

Now, I will give a0 ~ a9 and two positive integers k and m ,and could you help Lele to caculate f(k)%m.

Input

The problem contains mutiple test cases.Please process to the end of file.
In each case, there will be two lines.
In the first line , there are two positive integers k and m. ( k<2*10^9 , m < 10^5 )
In the second line , there are ten integers represent a0 ~ a9.

Output

For each case, output f(k) % m in one line.

Sample Input

10 9999 1 1 1 1 1 1 1 1 1 1 20 500 1 0 1 0 1 0 1 0 1 0

Sample Output

45 104

描述

乐乐现在正在考虑一个简单的函数f(x)。

如果x < 10 f(x)= x。
如果x > = 10 f(x)= a0 * f(x - 1)+ a1 * f(2)+ a2 * f(x 3)+……+ a9 * f(10倍);
和人工智能(0 < =我< = 9)只能是0或1。

现在,我要给a0 ~ a9和两个正整数k和米,和你能帮乐乐各行各业f(k)%。

输入

这个问题包含多种测试用例。 请处理的文件。
在每种情况下,将会有两行。
在第一行中,有两个正整数k和m。(k < 2 * 10 ^ 9,m < 10 ^ 5)
在第二行,有十个整数代表a0 ~ a9。

输出

每种情况下,输出f(k)% m在一行。

样例输入

9999 1 1 1 1 1 1 1 1 1 1 500 1 0 1 0 1 0 1 0 1 0

样例输出

45 104年

这题很简单,不会做的都是SB;

#include<cstdio>
#include<cstring>
using namespace std;
long long a[20],m,n,b[20];
void f()
{
long long s1[14][14],s2[14][14],s3[14][14],sum=0;
long long i,j,k;
for (i=0;i<=9;i++)
for (j=0;j<=9;j++)
{
if (j==9)
{
s1[i][9]=a[9-i];
s2[i][9]=a[9-i];
}
else if (i==j+1)
{
s1[i][j]=1;
s2[i][j]=1;
}
else
{
s1[i][j]=0;
s2[i][j]=0;
}
}
m-=10;
while (m)
{
if (m&1)
{
memset(s3,0,sizeof(s3));
for (i=0;i<=9;i++)
for (j=0;j<=9;j++)
for (k=0;k<=9;k++)
s3[i][j]+=(s1[i][k]*s2[k][j])%n;
for (i=0;i<=9;i++)
for (j=0;j<=9;j++)
s2[i][j]=s3[i][j];
}
memset(s3,0,sizeof(s3));
for (i=0;i<=9;i++)
for (j=0;j<=9;j++)
for (k=0;k<=9;k++)
s3[i][j]+=(s1[i][k]*s1[k][j])%n;
for (i=0;i<=9;i++)
for (j=0;j<=9;j++)
s1[i][j]=s3[i][j];
m>>=1;
}
for (i=0;i<=9;i++)  sum=((b[i]*s2[i][9])%n+sum)%n;
printf("%lld\n",sum);
}
int main()
{
long long i,j;
while (~scanf("%lld%lld",&m,&n))
{
if (m==0&&n==0)  break;
for (i=0;i<10;i++) scanf("%lld",&a[i]);
for (i=0;i<10;i++) b[i]=i%n;
if (m<10) printf("%lld\n",m%n);
else f();

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: