Problem Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1117
Solution idea : take every digit and represent it in decimal and mod it.. update the result every time...if the final result is divisible by m than ans is YES.
Solution :
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll int bigmod(ll int b,ll int p,ll int m)
{
if(p==0) return 1%m;
else if(p%2==0)
{
ll int y= bigmod(b,p/2,m);
return (y*y)%m;
}
else return (b*(bigmod(b,p-1,m)))%m;
}
ll int s[10002];
int main()
{
char ch;
ll int index,m=131071,res,val;
while(scanf("%ch",&ch)==1)
{
index=0;
s[index]=ch-48;
index++;
res=0;
val=0;
while(scanf("%ch",&ch)==1)
{
if(ch=='#') break;
if(ch=='1'||ch=='0')
{
s[index]=ch-48;
index++;
}
}
getchar();
for(ll int i=0; i<index; i++)
{
val=(s[i]*bigmod(2,index-1-i,m))%m;
res=res+val;
}
if(res%m==0) printf("YES\n");
else printf("NO\n");
}
}
Solution idea : take every digit and represent it in decimal and mod it.. update the result every time...if the final result is divisible by m than ans is YES.
Solution :
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll int bigmod(ll int b,ll int p,ll int m)
{
if(p==0) return 1%m;
else if(p%2==0)
{
ll int y= bigmod(b,p/2,m);
return (y*y)%m;
}
else return (b*(bigmod(b,p-1,m)))%m;
}
ll int s[10002];
int main()
{
char ch;
ll int index,m=131071,res,val;
while(scanf("%ch",&ch)==1)
{
index=0;
s[index]=ch-48;
index++;
res=0;
val=0;
while(scanf("%ch",&ch)==1)
{
if(ch=='#') break;
if(ch=='1'||ch=='0')
{
s[index]=ch-48;
index++;
}
}
getchar();
for(ll int i=0; i<index; i++)
{
val=(s[i]*bigmod(2,index-1-i,m))%m;
res=res+val;
}
if(res%m==0) printf("YES\n");
else printf("NO\n");
}
}
No comments:
Post a Comment