从后往前 把模后的值存在一个map里 每次有相同的值的时候就加入答案中
注意2 和 5是10的因数 要特殊处理
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<deque>
#include<iostream>
#include<map>
#define SF scanf
#define PF printf
using namespace std;
typedef long long LL;
const int MAXN = 100000;
int a[MAXN+10];
int n, S, W, p;
map <LL, LL> M;
void getA()
{
int g = S;
for(int i=0; i<n; i++) {
a[i] = (g/7) % 10;
if( g%2 == 0 ) { g = (g/2); }
else { g = (g/2) ^ W; }
}
}
int main()
{
while(SF("%d%d%d%d", &n, &S, &W, &p) && n+S+W+p)
{
getA();
LL ans = 0;
if(p == 2 || p == 5) { // 10的因数
int cnt = 0;
for(int i = 0; i < n; i++)
{
if(a[i]) cnt++;
if(a[i] % p == 0) ans += cnt;
}
cout << ans << '\n'; continue;
}
LL k = 1, sum = 0;
M.clear();
for(int i = n-1; i >= 0; i--)
{
sum = (k * a[i] + sum) % p;
int tmp = M[sum]++;
if(a[i]) ans += tmp;
if(a[i] && sum == 0) ans++;
k *= 10; k %= p;
}
cout << ans << '\n';
}
}