未完成的两道题2
include
include
using namespace std;
const int N = 1000;
long long p;
int n;
int a[N],y[N],K[N],x;
template
inline void read(Tp &x) {
x = 0; bool fg = 0; char ch = getchar();
while (ch < '0' || ch > '9') { if (ch == '-') fg ^= 1; ch = getchar();}
while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (Tp)(ch ^ 48), ch = getchar();
if (fg) x = -x;
}
long long expt(long long xx,long long nn)
{
long long ans = 1;
while(nn)
{
if(nn&1)
{
ans*=xx%p;
}
nn>>=1;
xx *= xx %p;
}
ans %= p;
return ans;
}
void cal()
{
long long ans = 0;
long long sum = 0;
long long xx = x;
for(int i = 0;i<n+1;i++)
{
if(a[i] == 0)
{
continue;
}
sum = 0;
if(y[i] == 1)
{
sum = K[i];
}
else
{
sum =(long long) ((double)(expt(y[i],K[i]+1) -1)/(y[i] - 1)) %p;
}
if(i == 0)
{
ans+= (long long)sum*a[i] %p;
}
else
{
ans+= (long long)sum*a[i]*xx %p;
xx = (long long)x*xx %p;
}
}
ans %= p;
cout<<ans;
}
int main()
{
cin>>n>>x>>p;
for(int i = 0;i<n+1;i++)
{
read(a[i]);
read(y[i]);
read(K[i]);
}
cal();
return 0;
}