这是我今天遇到最奇怪的问题,希望有人帮我解释一下。。。
一开始我能得90分:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
using namespace std;
#define duke(i,a,n) for(int i = a;i <= n;i++)
#define lv(i,a,n) for(int i = a;i >= n;i--)
#define clean(a) memset(a,0,sizeof(a))
typedef long long ll;
const int INF = << ;
const ll mod = ; typedef double db;
template <class T>
void read(T &x)
{
char c;
bool op = ;
while(c = getchar(), c < '' || c > '')
if(c == '-') op = ;
x = c - '';
while(c = getchar(), c >= '' && c <= '')
x = x * + c - '';
if(op) x = -x;
}
template <class T>
void write(T x)
{
if(x < ) putchar('-'), x = -x;
if(x >= ) write(x / );
putchar('' + x % );
}
ll n,s = ;
ll p[],x,ans = ;
ll num = ;
int main()
{
read(n);
duke(i,,n)
{
read(x);
if(x == )
{
p[++num] = x;
}
else
{
s = ;
while (p[num] != )
{
s += p[num];
s = s % mod;
num--;
}
if(s == )
{
p[num] = ;
}
else
{
p[num] = s * % mod;
}
}
}
ll tot = ;
duke(i,,num)
{
// cout<<p[i]<<endl;
tot += p[i];
tot %= mod;
}
write(tot);
return ;
}
然而并不知道为什么。。。找了个题解,发现没啥区别,然后就改了一下num的顺序,结果变成80,最神奇的是之前错的那个点对了,然而另两个点错了。。。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
using namespace std;
#define duke(i,a,n) for(int i = a;i <= n;i++)
#define lv(i,a,n) for(int i = a;i >= n;i--)
#define clean(a) memset(a,0,sizeof(a))
typedef long long ll;
const int INF = << ;
const ll mod = ;
typedef double db;
template <class T>
void read(T &x)
{
char c;
bool op = ;
while(c = getchar(), c < '' || c > '')
if(c == '-') op = ;
x = c - '';
while(c = getchar(), c >= '' && c <= '')
x = x * + c - '';
if(op) x = -x;
}
template <class T>
void write(T x)
{
if(x < ) putchar('-'), x = -x;
if(x >= ) write(x / );
putchar('' + x % );
}
ll n,s = ;
ll p[],x,ans = ;
ll num = ;
int main()
{
// freopen("1.in","r",stdin);
// freopen("1.out","w",stdout);
read(n);
p[] = ;
p[] = ;
clean(p);
duke(i,,n)
{
read(x);
if(x == )
{
p[num++] = x;
}
if(x == )
{
s = ;
while (p[num - ] != )
{
s += p[num - ];
s = s % mod;
num--;
}
/*if(i == n)
{
printf("%lld\n",s);
cout<<num - 1<<endl;
}*/
if(s == )
{
p[num - ] = ;
}
else
{
p[num - ] = (s * ) % mod;
}
/*if(i == n)
{
printf("%lld\n",p[num - 1]);
cout<<num - 1<<endl;
}*/
}
}
ll tot = ;
duke(i,,num - )
{
// cout<<p[i]<<endl;
tot += p[i];
tot %= mod;
}
write(tot);
return ;
}
蒙了,然后一点点de,到最后也没看出来。通过一些手段发现了是算和的时候出问题了,然而并不知道为什么。。。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
using namespace std;
#define duke(i,a,n) for(int i = a;i <= n;i++)
#define lv(i,a,n) for(int i = a;i >= n;i--)
#define clean(a) memset(a,0,sizeof(a))
typedef long long ll;
const int INF = << ;
const ll mod = ;
typedef double db;
template <class T>
void read(T &x)
{
char c;
bool op = ;
while(c = getchar(), c < '' || c > '')
if(c == '-') op = ;
x = c - '';
while(c = getchar(), c >= '' && c <= '')
x = x * + c - '';
if(op) x = -x;
}
template <class T>
void write(T x)
{
if(x < ) putchar('-'), x = -x;
if(x >= ) write(x / );
putchar('' + x % );
}
ll n,s = ;
ll p[],x,ans = ;
ll num = ;
int main()
{
// freopen("1.in","r",stdin);
// freopen("1.out","w",stdout);
read(n);
p[] = ;
p[] = ;
clean(p);
duke(i,,n)
{
read(x);
if(x == )
{
p[num] = x;
num++;
}
if(x == )
{
s = ;
while (p[num - ] != )
{
s += p[num - ];
s = s % mod;
num--;
}
if(s == )
{
p[num - ] = ;
}
else
{
p[num - ] = (s * ) % mod;
}
}
}
for(int i=;i<num;i++)
{
// cout<<p[i]<<endl;
p[] += p[i];
p[] = p[] % ;
}
cout<<p[];
return ;
}
/*
6
0
0
1
1
0
1
( ( ) ) ( )
0 0 1 2 0 1
*/
就是把
ll tot = ;
duke(i,,num - )
{
// cout<<p[i]<<endl;
tot += p[i];
tot %= mod;
}
write(tot);
换成
for(int i=;i<num;i++)
{
// cout<<p[i]<<endl;
p[] += p[i];
p[] = p[] % ;
}
cout<<p[];
就对了!!!震惊!!!为什么?求大佬解释。。。