首页 > 其他分享 >T287328 求和(正经题目)(有数据) 题解

T287328 求和(正经题目)(有数据) 题解

时间:2022-10-28 09:25:22浏览次数:52  
标签:__ node cnt 求和 题解 ans int128 T287328 for1

题目

30分 暴力:

#include<bits/stdc++.h>
#define for1(i,a,b) for(int i=a;i<=b;i++)
#define ll long long
using namespace std;
ll  gcd(ll a,ll b)
{
	if(b==0) return a;
	return gcd(b,a%b);
}
ll n,k,ans;
int main()
{
//	freopen("data3.in","r",stdin); 
//	freopen("data3.out","w",stdout); 
	cin>>n>>k;
	for1(i,1,n) 
	{
		ans+=gcd(i,k);
	}
	cout<<ans<<endl;
	return 0;
 } 

100 分 找到k的每个因子,然后容斥计算一下:

#include<bits/stdc++.h>
#define for1(i,a,b) for(ll i=a;i<=b;i++)
#define ll long long
using namespace std;
ll n,k,cnt;
ll jl[500005],ans;
struct node{
	ll x;
	ll y;
}a[500005];

bool cmp(node x,node y)
{
	return x.x<y.x;
}
int main()
{
//	freopen("data10.in","r",stdin); 
//	freopen("data10.out","w",stdout); 
	cin>>n>>k;
	for1(i,1,sqrt(k))
	{
		if(k%i==0)
		{
		a[++cnt].x=i;
		a[cnt].y=n/i;
		
		a[++cnt].x=k/i;
		a[cnt].y=n/(a[cnt].x);
		}
	}
	sort(a+1,a+cnt+1,cmp);
	for(int i=cnt;i>=1;i--)
	{
		
		for1(j,1,i-1)
		{
			if(a[i].x%a[j].x==0) a[j].y-=a[i].y;
		}
	}
	for1(i,1,cnt)
	    ans+=a[i].x*a[i].y;
	cout<<ans<<endl;
	return 0;
} 

120分 __int128/高精:

#include<bits/stdc++.h>
#define for1(i,a,b) for(ll i=a;i<=b;i++)
#define ll long long
using namespace std;
__int128 read(){
    __int128 x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-') f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
void print(__int128 x){
    if(x<0){
        putchar('-');
        x=-x;
    }
    if(x>9) print(x/10);
    putchar(x%10+'0');
}

__int128 n,k,cnt;
__int128 jl[5000005],ans;
struct node{
	__int128 x;
	__int128 y;
}a[5000005];

bool cmp(node x,node y)
{
	return x.x<y.x;
}
int main()
{
//	freopen("data12.in","r",stdin); 
//	freopen("data12.out","w",stdout); 
	n=read();
	k=read();
	ll ji=k;
	for1(i,1,sqrt(ji))
	{
		if(k%i==0)
		{
		a[++cnt].x=i;
		a[cnt].y=n/i;
		
		a[++cnt].x=k/i;
		a[cnt].y=n/(a[cnt].x);
		}
	}
	sort(a+1,a+cnt+1,cmp);
	for(int i=cnt;i>=1;i--)
	{
		
		for1(j,1,i-1)
		{
			if(a[i].x%a[j].x==0) a[j].y-=a[i].y;
		}
	}
	for1(i,1,cnt)
	    ans+=a[i].x*a[i].y;
	print(ans);
	return 0;
} 

标签:__,node,cnt,求和,题解,ans,int128,T287328,for1
From: https://www.cnblogs.com/yyx525jia/p/16834702.html

相关文章

  • Nauuo and Binary Tree 题解
    linkSolution超级有意思的题目,可惜还是做不出来。/kk我们首先看出我们可以求出每一个点的深度。然后考虑深度从小到大考虑对于每一个点找出它的父亲。你发现如果求出两......
  • new: 轮播图 | MDN上HTML的总结和CSS面试题解答,以及vue-admin/豆瓣一个静态页面的实现
    主要参看oppo官网https://www.oppo.com/cn/,实现以下功能一、轮播图https://www.cnblogs.com/WindrunnerMax/p/12638005.html通常是在首页读秒播放的图片,本次了解的是opp......
  • 力扣(leetcode) 67. 二进制求和(进制之间的转换)
    题目在这:​​https://leetcode-cn.com/problems/add-binary/​​题目分析:题目给了两个二进制表示的数,让你把他们相加然后返回二进制数。还是比较容易理解的。思路分析:二进......
  • 循环~分数序列求和
    题目描述有一分数序列:2/13/25/38/513/821/13......求出这个数列的前N项之和,保留两位小数。输入N输出数列前N项和1#include<stdio.h>2intmain(){3......
  • ssh连接长时间不操作自动断开问题解决
    解决方法:修改服务器ssh连接参数在服务器端编辑sshd服务的配置文件sudovim/etc/ssh/sshd_config在文件最后添加#每60秒连接一下客户端ClientAliveInterval60#......
  • CF1754 题解
    比赛链接:https://codeforces.com/contest/1754题解:AB水题//bySkyRainWind#include<cstdio>#include<vector>#include<cstring>#include<iostream>#include......
  • 洛谷 P8595 「KDOI-02」一个网的路 蓝 题解
    定义树形\(DP\),这个挺明显的,我认为\(DP\)让读者理解的最重要的一步是:定义。所以我先详细说明\(f\)数组的定义,至于为什么这么定义后面再讲。\(f_{u,type}\),其中\(......
  • IDEA删除子项目后,在重建时出现的问题解决方法
    IDEA删除子项目后,在重建时出现的问题解决方法每次删除子项目后都会出现这样的问题发现java,resource里面没有变色,当你对pom,文件里面的内容进行添加依赖是,在点击idea—>f......
  • P5377 鸽鸽的分割 评论及c++题解
    P5377鸽鸽的分割1.原题连接2.评论下位红(划掉简单题只需要推导出公式或分类讨论就行了这里只给出公式解法根据题意在一个圆上确定n(n∈正整数)个点,求最多可被......
  • AtCoder Beginner Contest 201 题解
    vp情况:过了A到E,F没时间也不会。vp总结:ABC表现可以。D慢了一点,写之前大概考虑清楚每个变量或函数的意义,结构明晰才能更快的写出代码。E花的时间太长,原因......