Page 1 of 2
11155 - Be Efficient
Posted: Sat Jan 20, 2007 6:47 pm
by Karim
Can anyone tell me whats the idea of this problem
cause i am getting TLE
thanks
Posted: Sat Jan 20, 2007 7:13 pm
by emotional blind
try to find out order M solution, which i did in contest.
Posted: Sat Jan 20, 2007 7:18 pm
by emotional blind
Actually my algorithm was theta(N+M).
Posted: Sat Jan 20, 2007 7:25 pm
by Karim
my algorithm is trying all subsets and check if divisible by m
i think O( n ^ 2)
Posted: Sat Jan 20, 2007 7:32 pm
by emotional blind
thats why you got TLE
Posted: Sun Jan 21, 2007 7:37 am
by paulmcvn
Posted: Sun Jan 21, 2007 10:36 am
by rio
Getting WA..
Is sum 0 considered as Multiple of M?
What would be the result for sequence
with M = 2.
With considering sum 0 as Multiple of M, it will be 6.
Code: Select all
({1,0,1}, {0}, {1,0,1,0}, {1,0,1,0,0}, {0,0}, {0})
Without considering sum 0 as Multiple of M, it sould be 3.
Code: Select all
({1,0,1}, {1,0,1,0}, {1,0,1,0,0})
Which is right ? (maybe both wrong...)
Posted: Sun Jan 21, 2007 10:52 am
by rio
Ooops. I found my mistake and got AC.
I was assuming A <= M. Be carefull...
Anyway, it seems sum 0 sould be consider as Multiple of M.
ADD: I think paulmcvn post might be a little spoiler.
Posted: Sun Jan 21, 2007 6:37 pm
by emotional blind
rio wrote:ADD: I think paulmcvn post might be a little spoiler.
yes, I agree.

why WA?
Posted: Mon Jan 22, 2007 1:03 pm
by nut
It seems right but I can't find the bug.
Code: Select all
#include <iostream>
#define MAXN 10000
using namespace std;
long long t, cc, a, b, c, m, n, i, r, res;
long long x[MAXN];
long long s[MAXN];
long long mod[MAXN];
int main()
{
cin >> t;
for (cc=0; cc<t; cc++)
{
cin >> a >> b >> c >> m >> n;
x[0] = a;
for (i=1; i<n; i++)
x[i] = ((x[i-1] * b + c) % m) + 1;
s[0] = x[0];
for (i=1; i<n; i++)
s[i] = (s[i-1] + x[i]) % m;
for (i=0; i<MAXN; i++)
mod[i] = 0;
for (i=0; i<n; i++)
mod[s[i] % m]++;
res = 0;
for (i=0; i<m; i++)
res += (mod[i]*(mod[i]-1) / 2);
cout << "Case " << cc+1 << ": " << res << endl;
}
return 0;
}
Posted: Mon Jan 22, 2007 1:15 pm
by cypressx
just add 'mod[0]++;' after res=0
Posted: Wed Jan 24, 2007 1:26 am
by artie
paulmcvn wrote:First, generate the sequence x like the problem description
Then, generate the sequence s,
where s = x[0] + x[1] + ... x
While computing, you can set s = (s[i-1] + x) % m; , because we only deal with the remainder modulo m
For each 0<=r<m, count the number of s, so that s % m = r
That means, you have to iterate through s, and set
mod[s % m]++;
Moreover, set mod[0]++;
Then, the result is sum ( mod*(mod[i] - 1) div 2, i=0..m-1)
That's simply because each subsequence can be represented as s[i] - s[j] for some i,j.
Would you please give a detailed explanation about sum ( mod[i]*(mod[i] - 1) div 2, i=0..m-1) formula? I can't get it 
Posted: Wed Jan 24, 2007 7:10 am
by rio
Feel this thread too SPOILic ...
>paulmcvn
Please remove or edit your post. It's just not only me that feeling it spoilic.
>nut
Don't forget removing your code after AC.
>artie
Don't quote a post that might be spoilic
-----
(Is there a English term "spoilic" ..? Sory for my lack of English)
Posted: Thu Mar 22, 2007 1:34 pm
by temper_3243
First, generate the sequence x
like the problem description
Then, generate the sequence s,
where s = x[0] + x[1] + ... x
While computing, you can set s = (s[i-1] + x) % m; , because we only deal with the remainder modulo m
For each 0<=r<m, count the number of s, so that s % m = r
That means, you have to iterate through s, and set
mod[s % m]++;
Moreover, set mod[0]++;
Why should this be done ?
Then, the result is sum ( mod*(mod[i] - 1) div 2, i=0..m-1)
That's simply because each subsequence can be represented as s[i] - s[j] for some i,j.[/quote]
Would you please give a detailed explanation about sum ( mod[i]*(mod[i] - 1) div 2, i=0..m-1) formula? I can't get it 
I don't understand this quite why it should be done. rationale behind this
Posted: Thu Mar 22, 2007 4:30 pm
by helloneo
nC2 = n*(n-1)/2
The formula you're wondering above has come from it..
The reason we do this is also explained here..
That's simply because each subsequence can be represented as s - s[j] for some i,j
and mod[0] is kind of special because mod[0] itself can be the sequence we're looking for..