UVA 12546-LCM Pair Sum


%5Blcm(i%2Cj)%3Dn%5D%7D&preview=true)

%3D%5Csum_%7Bi%3D1%7D%5E%7Bn%7D%5Csum_%7Bj%3Di%7D%5E%7Bn%7D(i%2Bj)%5Blcm(i%2Cj)%3Dn%5D%3D%5Csum_%7Bi%3D1%7D%5E%7Bn%7D%5Csum_%7Bj%3D1%7D%5E%7Bi%7D(i%2Bj)%5Blcm(i%2Cj)%3Dn%5D%7D&preview=true)
%3D%5Csum_%7Bi%3D1%7D%5E%7Bn%7D%5Csum_%7Bj%3D1%7D%5E%7Bn%7D(i%2Bj)%5Blcm(i%2Cj)%3Dn%5D%3D2f(n)-2%5Ccdot%20n%7D&preview=true)
%3D4%5Ccdot%20p%2B2%5Cquad%20F(p%5Ea)%3D2%5Ccdot%20(a%2B1)%5Ccdot%20p%5Ea%2B%5Cfrac%7B2%5Ccdot%20p%5Ea-2%5Ccdot%20p%7D%7Bp-1%7D%2B2%7D&preview=true)
%3D%5Cfrac%7B%5Cprod_%7Bi%3D1%7D%5E%7Bk%7DF(p%5E%7Ba_i%7D)%7D%7B2%5E%7Bk-1%7D%7D%20%5Cquad%20f(n)%3Dn%2B%5Cfrac%7B%5Cprod_%7Bi%3D1%7D%5E%7Bk%7DF(p%5E%7Ba_i%7D)%7D%7B2%5E%7Bk%7D%7D%7D&preview=true)

#include<bits/stdc++.h>
#define me(a,x) memset(a,x,sizeof(a))
#define sc scanf
#define itn int
#define IN freopen("in.txt","r",stdin);
#define OUT freopen("out.txt","w",stdout);
#define STR clock_t startTime = clock();
#define END clock_t endTime = clock();cout << double(endTime - startTime) / CLOCKS_PER_SEC *1000<< "ms" << endl;
using namespace std;
const int N=105;
const long long mod=1e9+7;
const long long mod2=998244353;
const int oo=0x7fffffff;
const int sup=0x80000000;
typedef long long ll;
typedef unsigned long long ull;
template <typename it>void db(it *begin,it *end){while(begin!=end)cout<<(*begin++)<<" ";puts("");}
template <typename it>
string to_str(it n){string s="";while(n)s+=n%10+'0',n/=10;reverse(s.begin(),s.end());return s;}
template <typename it>int o(it a){cout<<a<<endl;return 0;}
inline ll mul_64(ll x,ll y,ll c){return (x*y-(ll)((long double)x/c*y)*c+c)%c;}
ll ksm(ll a,ll b,ll c){ll ans=1;for(;b;b>>=1,a=a*a%c)if(b&1)ans=ans*a%c;return ans;}
void exgcd(ll a,ll b,ll &x,ll &y){if(!b)x=1,y=0;else exgcd(b,a%b,y,x),y-=(a/b)*x;}
const int inv2=mod+1>>1;
ll f[N];
int main(){
int t;
cin>>t;
for(int cas=1;cas<=t;cas++){
int c;sc("%d",&c);
ll n=1;ll ans=1;
for(int i=1;i<=c;i++){
int p,a;
sc("%d%d",&p,&a);
n=n*ksm(p,a,mod)%mod;
if(a==1)f[i]=4LL*p+2;
else{
f[i]=2;
f[i]+=2LL*(a+1)%mod*ksm(p,a,mod)%mod+2LL*ksm(p-1,mod-2,mod)%mod*p%mod*(ksm(p,a-1,mod)-1)%mod;
f[i]=(f[i]%mod+mod)%mod;
}
}
for(int i=1;i<=c;i++){
ans=ans*f[i]%mod;
}
ans=ans*ksm(ksm(2,c,mod),mod-2,mod)%mod;
ans+=n;
printf("Case %d: %lld\n",cas,(ans+mod)%mod);
}
}