Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is this code correct.  

I want to find modulo where, n=10^9+7.  

 <pre>MY CODE IS
#include <bits/stdc++.h>
using namespace std;


int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t,n,ans;
cin>>t;
while(t--)
{
cin>>n;

ans = ((2%1000000007) * (pow(3,n)%1000000007))%1000000007;


cout<<ans-1<<endl;



}
return 0;

}


error is:

||=== Build file: "no target" in "no project" (compiler: unknown) ===|
/home/jatin/programs/tri.cpp||In function ‘int main()’:|
/home/jatin/programs/tri.cpp|16|error: invalid operands of types ‘__gnu_cxx::__promote_2<int, long long int, double, double>::__type {aka double}’ and ‘int’ to binary ‘operator%’|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|




What I have tried:

pow(a,b)%n
gives an error.
Plzz tell me the reason.
Posted
Updated 26-Nov-17 20:16pm
v2
Comments
Patrice T 27-Nov-17 0:32am    
give error message.
show full routine.
jatinp510 27-Nov-17 0:38am    
||=== Build file: "no target" in "no project" (compiler: unknown) ===|
/home/jatin/programs/tri.cpp||In function ‘int main()’:|
/home/jatin/programs/tri.cpp|16|error: invalid operands of types ‘__gnu_cxx::__promote_2<int, long long int, double, double>::__type {aka double}’ and ‘int’ to binary ‘operator%’|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
jatinp510 27-Nov-17 0:38am    
MY CODE IS
#include <bits/stdc++.h>
using namespace std;


int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t,n,ans;
cin>>t;
while(t--)
{
cin>>n;

ans = ((2%1000000007) * (pow(3,n)%1000000007))%1000000007;


cout<<ans-1<<endl;



}
return 0;

}
Patrice T 27-Nov-17 1:35am    
Use Improve question to update your question.
So that everyone can pay attention to this information.

1 solution

% is an int operator
pow is a double function
Convert everything to double and then use fmod instead of %
 
Share this answer
 
v2
Comments
jatinp510 27-Nov-17 2:14am    
i don't lnow how to do this.
plzz give me soln.
Patrice T 27-Nov-17 2:52am    
Search fmod and read documentation.
jatinp510 27-Nov-17 3:12am    
sir....
loves ur way to teach.
Always do research...
thnx...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900