数论整除——cf1059D
2024-09-06 07:53:56
用map是卡着过去的。。题解用vector+离散化后常数小了十倍。。
总之就是把所有模数给保存下来然后离散化,再去匹配一下即可,最后有个细节
自己的
#include<bits/stdc++.h>
using namespace std;
#define ll int
#define maxn 200005
ll n,k,a[maxn];
map<ll,ll>mp[];
int main(){
cin>>n>>k;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
ll tmp=a[i],len=;
while(tmp)len++,tmp/=;
mp[len][a[i]%k]++;
}
ll ans=;
for(int i=;i<=n;i++){
ll tmp=a[i],tmp2=a[i],len=;
while(tmp2)len++,tmp2/=;
for(int j=;j<=;j++){
tmp=tmp*%k;
ans+=mp[j][(k-tmp)%k];
if(j==len && a[i]%k==(k-tmp)%k)ans--;
} }
cout<<ans<<endl;
}
题解的
#include <bits/stdc++.h> #define forn(i, n) for (int i = 0; i < int(n); i++) typedef long long li; using namespace std; const int N = * + ;
const int LOGN = ; int n, k;
int a[N];
int len[N];
vector<int> rems[LOGN];
int pw[LOGN]; int main() {
scanf("%d%d", &n, &k);
forn(i, n) scanf("%d", &a[i]); pw[] = ;
forn(i, LOGN - )
pw[i + ] = pw[i] * % k; forn(i, n){
int x = a[i];
while (x > ){
++len[i];
x /= ;
}
rems[len[i]].push_back(a[i] % k);
} forn(i, LOGN)
sort(rems[i].begin(), rems[i].end()); li ans = ;
forn(i, n){
for (int j = ; j < LOGN; ++j){
int rem = (a[i] * li(pw[j])) % k;
int xrem = (k - rem) % k;
auto l = lower_bound(rems[j].begin(), rems[j].end(), xrem);
auto r = upper_bound(rems[j].begin(), rems[j].end(), xrem);
ans += (r - l);
if (len[i] == j && (rem + a[i] % k) % k == )
--ans;
}
} printf("%lld\n", ans);
return ;
}
最新文章
- [LeetCode] Remove Duplicates from Sorted Array
- EditText监听键盘输入
- [UML][转]UML类图符号 各种关系说明以及举例
- C# 跨线程访问或者设置UI线程控件的方法
- unauthenticated user reading from net
- Memcache+Tomcat9集群实现session共享(非jar式配置, 手动编写Memcache客户端)
- jsf taglib定义函数
- Web网页中动态数据区域的识别与抽取 Dynamical Data Regions Identification and Extraction in Web Pages
- Ajax基础知识(二)
- Android 再按一次退出程序三种办法
- NEO从入门到开窗(2) - 智能合约的面相
- ROS机器人程序设计(原书第2版)补充资料 (壹) 第一章 ROS系统入门
- pig基础知识总结
- 找jar包的网站 还没用过2017.12.19
- Django ORM多表操作
- Elasticsearch简介和安装对比
- qt 在窗口上画框
- System.ComponentModel.DataAnnotations.Schema.TableAttribute 同时存在于EntityFramework.dll和System.ComponentModel.DataAnnotations.dll中
- 资源中心的ES 服务的COM.IFLYTEK.ERSP.API.RESOURCEAPI 接口注册ZOOKEEPER失败,解决记录
- luogu1514 [NOIp2010]引水入城 (bfs+记忆化搜索)