题解 | 【模板】分数取模

【模板】分数取模

https://www.nowcoder.com/practice/23839ef20d5f4dbaa9664daa51864291

#include<bits/stdc++.h>
using namespace std;

using ui=unsigned int;
using ll=long long;
using ull=unsigned long long;
using i128=__int128_t;
using u128=__uint128_t;
using ld=long double;

const ll mod=1e9+7;

ll power(ll a,ll b,ll p)
{
	ll ans=1;
	while(b>0)
	{
		if((b&1)==1) ans=(ans*a)%p;
		a=(a*a)%p;
		b>>=1;
	}
	return ans;
}

void solve()
{//利用乘法快速幂求出b在mod1e9+7下的逆元 然后取模计算即可
//注意当a<0时 可以让a加mod直到为正数
	ll a,b;
	cin >> a >> b;
	if(a<0) while(a<0) a+=mod;
	ll ans=((a%mod)*(power(b,mod-2,mod)))%mod;
	cout << ans << "\n";
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int t=1;
	cin >> t;
	
	while(t--)
	{
		solve();
	}
	return 0;
}

全部评论

相关推荐

牛客82834144...:同推荐架构,hr面给口头oc了嘛还是说的要和业务讨论一下))我刚面完hr面说要和业务讨论一下,好紧张)
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务