当前位置:网站首页>Xiao Sha's arithmetic problem solving Report
Xiao Sha's arithmetic problem solving Report
2022-07-05 15:25:00 【wch(】
Xiao Sha's arithmetic problem solving report
label : Inverse element


source : Cattle from
Their thinking :
Start with an example ,
Give the formula 1+1+3 * 1+1 * 6 * 7;
We can divide the formula into 1,1,3 * 1,1 * 6 * 7, Four parts exist num 1~4 in
Set the pointer number to mark the group to which each number belongs , Remember that the sum at this time is ans;
When we Jiang's sixth digit 6 Change it to 7 when ,
First of all, from the ans Subtract from num[4], Ran Ling num[4] / 6 * 7, Re order ans += num[4] that will do
Then I found myself digging
Inverse element
The problem is that the subject data is too large and modular operation is carried out , The order of modulo and division cannot be changed
For example, we give the title mod Reduce to 5
Calculate according to the above method
num[4]=((16%5)7)%5=2
After change num[4]=2 / 6 * 7 % 5 =
2/6 In addition, the problems are exposed To prevent this, we use the inverse element
I am also a beginner of inverse yuan , Is not very good , Don't understand
I mainly refer to the following article
https://zhuanlan.zhihu.com/p/378728642
According to Fermat's theorem When a And b Coprime :
Apply it to the above example to find 6 The inverse element of is 216;
num[4]=2216%57%5=4;
num[4]=7*7%5=4;
That's right
We can make bold guesses 1e9+7 Prime number , Get by fast power a model b Inverse element
Code implementation :
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9+7;
const int N = 1e6+8;
char s[N];
ll num[N],pos[N],n,q,a[N];
ll ksm(ll a, ll b) {
ll ans = 1;
while(b){
if (b & 1) (ans *= a) %= mod;
(a *= a) %= mod;
b >>= 1;
}
return ans;
}
int main()
{
cin>>n>>q;
scanf("%s",s+1);
for(int i=1;i<=n;i++) cin>>a[i];
int cnt=1;
num[1]=a[1];
pos[1]=1;
for(int i=2;i<=n;i++){
if(s[i-1]=='*'){
pos[i]=cnt;
num[cnt]=num[cnt]*a[i]%mod;
}
else{
num[++cnt]=a[i];
pos[i]=cnt;
}
}
ll ans = 0,x=0,y=0;
for(int i=1;i<=cnt;i++) (ans+=num[i]) %= mod;
while(q--){
cin>>x>>y;
ans=(ans-num[pos[x]]+mod)%mod;
num[pos[x]]=num[pos[x]]*ksm(a[x],mod-2)%mod*y%mod;
ans=(ans+num[pos[x]])%mod;
a[x]=y;
cout<<ans<<endl;
}
}
边栏推荐
- Magic methods and usage in PHP (PHP interview theory questions)
- Coding devsecops helps financial enterprises run out of digital acceleration
- Can I pass the PMP Exam in 20 days?
- P6183 [USACO10MAR] The Rock Game S
- Super wow fast row, you are worth learning!
- 机器学习框架简述
- Reasons and solutions for redis cache penetration and cache avalanche
- Bugku's eyes are not real
- Good article inventory
- ionic cordova项目修改插件
猜你喜欢

Appium自动化测试基础 — APPium基础操作API(二)

Bugku's steganography

超越PaLM!北大碩士提出DiVeRSe,全面刷新NLP推理排行榜
![P6183 [USACO10MAR] The Rock Game S](/img/f4/d8c8763c27385d759d117b515fbf0f.png)
P6183 [USACO10MAR] The Rock Game S

Bugku cyberpunk

Bugku easy_ nbt

你童年的快乐,都是被它承包了

How can I quickly check whether there is an error after FreeSurfer runs Recon all—— Core command tail redirection

机器学习笔记 - 灰狼优化

Reasons and solutions for redis cache penetration and cache avalanche
随机推荐
P6183 [USACO10MAR] The Rock Game S
Where is the operation of convertible bond renewal? Is it safer and more reliable to open an account
easyOCR 字符识别
Dark horse programmer - software testing -10 stage 2-linux and database -44-57 why learn database, description of database classification relational database, description of Navicat operation data, de
sql server学习笔记
Common MySQL interview questions
How can I quickly check whether there is an error after FreeSurfer runs Recon all—— Core command tail redirection
Huawei Hubble incarnation hard technology IPO harvester
Reproduce ThinkPHP 2 X Arbitrary Code Execution Vulnerability
maxcompute有没有能查询 表当前存储容量的大小(kb) 的sql?
Leetcode: Shortest Word Distance II
Can gbase 8A view the location of SQL statement history?
[JVM] operation instruction
Ionic Cordova project modification plug-in
Cartoon: what are the attributes of a good programmer?
1330: [example 8.3] minimum steps
ICML 2022 | 探索语言模型的最佳架构和训练方法
Easyocr character recognition
Aike AI frontier promotion (7.5)
Jmeter性能测试:ServerAgent资源监控