当前位置:网站首页>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;
}
}
边栏推荐
- [recruitment position] infrastructure software developer
- Bugku alert
- R 熵权法计算权重及综合得分
- MySQL 巨坑:update 更新慎用影响行数做判断!!!
- Mysql---- function
- lvgl 显示图片示例
- 数据库学习——数据库安全性
- Surpass palm! Peking University Master proposed diverse to comprehensively refresh the NLP reasoning ranking
- go学习 ------jwt的相关知识
- PHP high concurrency and large traffic solution (PHP interview theory question)
猜你喜欢
lv_ font_ Conv offline conversion
Reasons and solutions for redis cache penetration and cache avalanche
1330:【例8.3】最少步数
Crud de MySQL
Stop B makes short videos, learns Tiktok to die, learns YouTube to live?
JS knowledge points-01
"Sequelae" of the withdrawal of community group purchase from the city
Au - delà du PARM! La maîtrise de l'Université de Pékin propose diverse pour actualiser complètement le classement du raisonnement du NLP
Bugku's steganography
keep-alive
随机推荐
Jmeter性能测试:ServerAgent资源监控
wxml2canvas
MySQL之CRUD
Common MySQL interview questions (1) (written MySQL interview questions)
Ctfshow web entry explosion
The elimination strategy of redis
Example of lvgl display picture
做研究无人咨询、与学生不交心,UNC助理教授两年教职挣扎史
MySQL----函数
[recruitment position] infrastructure software developer
episodic和batch的定义
Cartoon: programmers don't repair computers!
Fr exercise topic - simple question
Usage and usage instructions of JDBC connection pool
CODING DevSecOps 助力金融企业跑出数字加速度
Want to ask the big guy, is there any synchronization from Tencent cloud Mysql to other places? Binlog saved by Tencent cloud MySQL on cos
Database learning - Database Security
P6183 [USACO10MAR] The Rock Game S
Stm32+bh1750 photosensitive sensor obtains light intensity
I want to inquire about how to ensure data consistency when a MySQL transaction updates multiple tables?