Sunday, March 2, 2014

codechef RESIST - "resistance" solution

codechef RESIST - "resistance" : http://www.codechef.com/problems/RESIST

codechef RESIST - "resistance" editorial : http://discuss.codechef.com/questions/3633/resist-editorial

codechef RESIST - "resistance" solution :  http://ideone.com/fGu9M6


#include <iostream>
#include <cstdio>
using namespace std;

unsigned int gcd(unsigned int u, unsigned int v) {
if(u==v) return u;
if(u==0) return v;
if(v==0) return u;
if(~u & 1) {
if(v & 1) return gcd(u>>1, v);
else return gcd(u>>1, v>>1)<<1;
}
if(~v & 1) return gcd(u, v>>1);
if(u>v) return gcd((u-v)>>1, v);
return gcd((v-u)>>1, u);
}

int main() {
int t, n;
long long m;
scanf("%d", &t);
while(t--) {
scanf("%d%lld", &n, &m);
long long p, q;
p=q=1;
for(int i=1; i<n; i++) {
p=p+q;
q=p+q;
p=p%m;
q=q%m;
}
printf("%lld/%lld\n", p, q);
}
return 0;
}

note: after every circuit p/q gets new p/q, which is equal to p=p+q, and q=p+2q; q=p+2q=p+q+q can be equal to q=p+q after assigning p=p+q;


No comments:

Post a Comment