2956: 模积和
2019-04-13 14:45发布
生成海报
以为是个大水题。。。
结果写了两小时
我数学真差.jpg
容易发现式子可以化成
然后拆开可以得到
(四个)五个式子,分别计算即可…
(懒得写公式了,自己推一下吧
然后你就能发现死活过不去…
原因是啥呢…给的这个模数竟然不是个质数。
那么考虑用exgcd来求解逆元就行了
c++代码如下:
#include
#define rep(i,x,y) for(register int i = x ;i <= y;++ i)
#define repd(i,x,y) for(register int i = x; i >= y ;-- i)
using namespace std;
typedef long long ll;
templateinline void read(T&x)
{
x = 0;char c;int sign = 1;
do { c = getchar(); if(c == '-') sign = -1; }while(!isdigit(c));
do { x = x * 10 + c - '0'; c = getchar(); }while(isdigit(c));
x *= sign;
}
const int mod = 19940417;
ll n,m,n1,n2;
ll ans,cal1,cal2,cal3;
inline ll get(ll x) {return ((x+1)*x%mod)*n1%mod;}
inline ll get2(ll x) {return ((2*x+1)*(x+1)%mod*x%mod)*n2%mod;}
void exgcd(ll a,ll b,ll&x,ll&y)
{
if(!b)
{
x = 1;y = 0;
return;
}
exgcd(b,a%b,y,x);
y -= a/b*x;
}
int main()
{
read(n); read(m);
ll x;
exgcd(1ll*2,mod,n1,x);n1 = (n1+mod)%mod;
exgcd(1ll*6,mod,n2,x);n2 = (n2+mod)%mod;
ans = n * m % mod * n %mod * m % mod;
for(int i = 1,lst;i <= n; i = lst + 1)
{
lst = n/(n/i);
cal1 = (cal1 + (get(lst) - get(i-1)) * (n/i)%mod)% mod;
}
for(int i = 1,lst;i <= m; i = lst + 1)
{
lst = m/(m/i);
cal2 = (cal2 + (get(lst) - get(i-1)) * (m/i)%mod)% mod;
}
cal3 = min(n,m) * n % mod * m %mod ;
for(int i = 1,lst;i <= min(n,m); i = lst + 1)
{
lst = min(m/(m/i),n/(n/i));
cal3 = (cal3 + (mod - m*(get(lst) - get(i-1))%mod * (n/i) % mod))% mod;
cal3 = (cal3 + (mod - n*(get(lst) - get(i-1))%mod * (m/i) %mod)) % mod;
cal3 = (cal3 + (get2(lst) - get2(i - 1)+mod)%mod * (n/i)%mod * (m/i) %mod) % mod;
}
printf("%lld
",((ans - cal3 - (m*m%mod*cal1%mod) - (n*n%mod*cal2%mod) + cal1 * cal2%mod) %mod + mod )%mod);
return 0;
}
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮