原错误代码:
#include <iostream>
#include <cstdio>
using namespace std;
const int N = 100010;
typedef long long ll;
ll S[N],cnt[N];
int main()
{
int n,k;
scanf("%d %d",&n,&k);
for (int i=1;i<=n;i++)
{
scanf("%lld",S[i]);
S[i] = S[i-1]+S[i];
}
ll res = 0;
cnt[0] = 1;
for (int i=1;i<=n;i++)
{
res+=cnt[S[i]%k];
cnt[S[i]%k]++;
}
printf("%lld",res);
return 0;
}
输出:
方法:利用exit(0)和二分的方法”,不断更改exit(0)位置进行调试
1,
#include <iostream>
#include <cstdio>
using namespace std;
const int N = 100010;
typedef long long ll;
ll S[N],cnt[N];
void f()
{
exit(0);
}
int main()
{
int n,k;
scanf("%d %d",&n,&k);
for (int i=1;i<=n;i++)
{
scanf("%lld",S[i]);
S[i] = S[i-1]+S[i];
}
ll res = 0;
cnt[0] = 1;
for (int i=1;i<=n;i++)
{
res+=cnt[S[i]%k];
cnt[S[i]%k]++;
}
printf("%lld",res);
f();
return 0;
}
结果:
2,
#include <iostream>
#include <cstdio>
using namespace std;
const int N = 100010;
typedef long long ll;
ll S[N],cnt[N];
void f()
{
exit(0);
}
int main()
{
int n,k;
scanf("%d %d",&n,&k);
for (int i=1;i<=n;i++)
{
scanf("%lld",S[i]);
S[i] = S[i-1]+S[i];
}
ll res = 0;
cnt[0] = 1;
f();
for (int i=1;i<=n;i++)
{
res+=cnt[S[i]%k];
cnt[S[i]%k]++;
}
printf("%lld",res);
return 0;
}
结果:
3,
#include <iostream>
#include <cstdio>
using namespace std;
const int N = 100010;
typedef long long ll;
ll S[N],cnt[N];
void f()
{
exit(0);
}
int main()
{
int n,k;
scanf("%d %d",&n,&k);
f();
for (int i=1;i<=n;i++)
{
scanf("%lld",S[i]);
S[i] = S[i-1]+S[i];
}
ll res = 0;
cnt[0] = 1;
for (int i=1;i<=n;i++)
{
res+=cnt[S[i]%k];
cnt[S[i]%k]++;
}
printf("%lld",res);
return 0;
}
结果:
说明错误在:
for (int i=1;i<=n;i++)
{
scanf("%lld",S[i]);
S[i] = S[i-1]+S[i];
}
ll res = 0;
cnt[0] = 1;
之间
定睛一看,scanf("%lld",S[i]);
忘记加&了!