<Newcoder> n方格染色问题

 这个问题很意思

奇数 m*n^(m/2+1)  偶数  m/2*n^(m/2+1) + m/2*n^(m/2 )

:ans += gcd(n,i)   不同i不同数的求和

: 最后  :可能是死的,看了好久没看懂 ans*pow(m*2%M,M-2)%M

 

题目描述

最近古yu迷上了《恋与制作人》,天天嚷嚷着白起我老公(死gay死gay的),白起又向古yu提出了一个问题:
给你n种卡牌(数量无限),将其摆成长度为m的圆环的方案数。
(如果这个环可以通过若干次旋转或者反方向读((abc)(cba)是一样的),则认为他们是一样的)
这时古yu一脸茫然,大哭特苦不能守护白起了。古yu希望你能帮助他解决这个问题去守护白起。

输入描述:

第一行一个t(0<t<=500)
接下来t行,每行俩个正整数n,m(1<=n,m<=10000)

输出描述:

一个答案加换行(答案可能很大,所以取模1000000007)
示例1

输入

2
3 4
1 2

输出

21
1

说明

对于第一个样例有21种不同的方法
/ aaaa / aaab / aaac / aabb / aabc / aacc / abab /
/ abac / abbb / abbc / abcb / abcc / acac / acbc /
/ accc / bbbb / bbbc / bbcc / bcbc / bccc / cccc

#include<iostream>
using namespace std ;
typedef long long ll ;
#define f(i,l,r) for(int i=l;i<=r;++i)
#define g(i,l,r) for(int i=l;i>=r;--i)
  ll M = 1e9+7;
ll gcd(ll a,ll b){
    return b?gcd(b,a%b):a;
}
ll pow (ll a ,ll b){
	ll  ans =1;
	while(b){
		if(b%2==1)
			ans =ans*a%M;
		a=a*a%M;
		b>>=1;
	}
	return ans;
}

ll slove(ll n,ll m)
{
	ll ans=0;
	if(m & 1) ans=m*pow(n,m/2+1)%M;
	else ans=((m/2*pow(n,m/2))%M+(m/2*pow(n,m/2+1))%M)%M;
	for(int i=0;i<m;++i) ans=(ans+pow(n,gcd(m,i)))%M;
	ans =  ans*pow(m*2%M,M-2)%M ;
	return ans ;
} 

int main(){
	ll t ,a,b;
	cin>>t ;
	while(t--){
		cin>>a>>b;
		cout<<slove(a,b)<<endl;
	}


	return 0;
}

未来的我一定会感谢正在努力的现在的我!


我不需要商品信息在js中产生,我只要在html中产生即可,比如我原来那样: 2B铅笔(12支笔) 优质木质拖拽,书写顺畅,不粘附 ¥15.00 - 2 + ¥30 删除 <tr id="product2"> <td> <img src="img/pic2.jpg"/> </div> </td> <td> <div class="ms-name">A5笔记本</div> <div class="ms1">优质纸张,方格设计,适合日常记录<div> </td> <td > <span class="original-price">¥12</span> </td> <td > <button class="btn1" onclick="updateQuantity(1, -1)">-</button> <input type="text" class="quantity-input" id="qty2" value="2" readonly> <button class="btn1" onclick="updateQuantity(1, 1)">+</button> </td> <td > <div class="jg" id="jg2">¥24</div> </td> <td > <button class="sc" onclick="removeProduct(2)"> 删除 </button> </td> </tr> <tr id="product3"> <td> <img src="img/pic3.jpg"/> </div> </td> <td> <div class="ms-name">彩色荧光笔(5色)</div> <div class="ms1">多彩荧光标记笔,不易洇染</div> </td> <td > <span class="original-price">¥18.00</span> </td> <td > <button class="btn1" onclick="updateQuantity(1, -1)">-</button> <input type="text" class="quantity-input" id="qty3" value="2" readonly> <button class="btn1" onclick="updateQuantity(1, 1)">+</button> </td> <td > <div class="jg" id="jg3">¥36</div> </td> <td > <button class="sc" onclick="removeProduct(3)"> 删除 </button> </td> </tr> <tr id="product4"> <td> <img src="img/pic4.jpg"/> </div> </td> <td> <div class="ms-name">橡皮擦(3块装)</div> <div class="ms1">高效擦除,不留痕迹,不易碎</div> </td> <td > <span class="original-price">¥6.00</span> </td> <td > <button class="btn1" onclick="updateQuantity(1, -1)">-</button> <input type="text" class="quantity-input" id="qty4" value="2" readonly> <button class="btn1" onclick="updateQuantity(1, 1)">+</button> </td> <td > <div class="jg" id="jg4">¥12</div> </td> <td > <button class="sc" onclick="removeProduct(4)"> 删除 </button> </td> </tr> <tr id="product5"> <td> <img src="img/pic5.jpg"/> </div> </td> <td> <div class="ms-name">直尺30cm</div> <div class="ms1">透明刻度清晰,耐用防折断</div> </td> <td > <span class="original-price">¥5</span> </td> <td > <button class="btn1" onclick="updateQuantity(1, -1)">-</button> <input type="text" class="quantity-input" id="qty5" value="2" readonly> <button class="btn1" onclick="updateQuantity(1, 1)">+</button> </td> <td > <div class="jg" id="jg5">¥10</div> </td> <td > <button class="sc" onclick="removeProduct(5)"> 删除 </button> </td> </tr> <tr id="product6"> <td> <img src="img/pic6.jpg"/> </div> </td> <td> <div class="ms-name">圆规</div> <div class="ms1">精密绘图工具,金属材质耐用</div> </td> <td > <span class="original-price">¥8</span> </td> <td > <button class="btn1" onclick="updateQuantity(1, -1)">-</button> <input type="text" class="quantity-input" id="qty6" value="2" readonly> <button class="btn1" onclick="updateQuantity(1, 1)">+</button> </td> <td > <div class="jg" id="jg6">¥16</div> </td> <td > <button class="sc" onclick="removeProduct(6)"> 删除 </button> </td> </tr> <tr id="product7"> <td> <img src="img/pic7.jpg"/> </div> </td> <td> <div class="ms-name">钢笔</div> <div class="ms1">经典款式,流畅书写,赠送墨囊</div> </td> <td > <span class="original-price">¥20.00</span> </td> <td > <button class="btn1" onclick="updateQuantity(1, -1)">-</button> <input type="text" class="quantity-input" id="qty7" value="2" readonly> <button class="btn1" onclick="updateQuantity(1, 1)">+</button> </td> <td > <div class="jg" id="jg7">¥30</div> </td> <td > <button class="sc" onclick="removeProduct(7)"> 删除 </button> </td> </tr> <tr id="product8"> <td> <img src="img/pic8.jpg"/> </div> </td> <td> <div class="ms-name">文件夹</div> <div class="ms1">A4尺寸,可装30页纸,多色可选</div> </td> <td > <span class="original-price">¥16.00</span> </td> <td > <button class="btn1" onclick="updateQuantity(1, -1)">-</button> <input type="text" class="quantity-input" id="qty8" value="2" readonly> <button class="btn1" onclick="updateQuantity(1, 1)">+</button> </td> <td > <div class="jg" id="jg8">¥32</div> </td> <td > <button class="sc" onclick="removeProduct(8)"> 删除 </button> </td> </tr> <tr id="product9"> <td> <img src="img/pic9.jpg"/> </div> </td> <td> <div class="ms-name">修正带</div> <div class="ms1">5mm宽度,8m长,修正精确无残留</div> </td> <td > <span class="original-price">¥10.00</span> </td> <td > <button class="btn1" onclick="updateQuantity(1, -1)">-</button> <input type="text" class="quantity-input" id="qty9" value="2" readonly> <button class="btn1" onclick="updateQuantity(1, 1)">+</button> </td> <td > <div class="jg" id="jg9">¥20</div> </td> <td > <button class="sc" onclick="removeProduct(9)"> 删除 </button> </td> </tr> <tr id="product10"> <td> <img src="img/pic10.jpg"/> </div> </td> <td> <div class="ms-name">便利贴套装</div> <div class="ms1">多色组合,3种尺寸,便于标记归类</div> </td> <td > <span class="original-price">¥14.00</span> </td> <td > <button class="btn1" onclick="updateQuantity(1, -1)">-</button> <input type="text" class="quantity-input" id="qty10" value="2" readonly> <button class="btn1" onclick="updateQuantity(1, 1)">+</button> </td> <td > <div class="jg" id="jg10">¥28</div> </td> <td > <button class="sc" onclick="removeProduct(10)"> 删除 </button> </td> </tr> </tbody> 另外请把新添加代码改成js入门级别的,不要太难
06-19
<think>我们不再使用数据数组,而是直接从HTML中读取商品信息。我们将为每个商品行添加data属性来存储单价,或者从已有的元素中获取。由于商品信息已经在HTML中,我们需要在JavaScript中动态获取这些信息。我们将为每个商品行设置一个data-price属性,这样我们可以直接从行元素中获取单价。或者,我们可以从class="original-price"的元素中获取文本,然后解析出价格。步骤:1.修改HTML结构,为每个商品行添加data-price属性,例如:<trid="product1"data-price="15.00">2.或者,我们也可以从.original-price元素中获取价格,这样就不需要修改HTML。考虑到不想修改太多HTML,我们选择从.original-price中获取价格。但是注意:在删除商品时,我们仍然需要知道该商品行的信息。我们设计如下:-在updateQuantity函数中,我们通过商品行元素获取价格和数量,然后计算小计。-在removeProduct中,我们直接删除该行,并重新计算总价。由于没有商品数组,我们需要遍历DOM中的商品行来计算总价。我们将为每个商品行添加一个类名,比如"product-row",这样便于选择所有商品行。修改HTML(在原有基础上添加):在每个<tr>标签上添加class="product-row",例如:<trid="product1"class="product-row">这样我们可以通过document.querySelectorAll('.product-row')获取所有商品行。具体实现:1.数量更新:-在updateQuantity函数中,我们接收两个参数:productId和change。-通过productId找到对应的input元素,更新数量(注意:数量不能小于0)。-然后调用updateSubtotal(productId)更新该商品的小计。2.更新小计:-在updateSubtotal函数中,根据productId找到商品行。-从该行中获取价格(从.original-price元素中获取文本,去掉¥符号,然后转成数字)。-获取该商品的数量(input的值)。-根据数量计算折扣(2件9折,3件及以上8折)。-计算小计并更新到.jg元素。3.删除商品:-在removeProduct函数中,根据productId找到商品行,添加动画效果然后移除。-然后调用calculateTotal()重新计算总价。4.计算总价(calculateTotal函数):-遍历所有.product-row(注意:被删除的商品行已经不在DOM中,所以不需要处理)。-对每个商品行,获取价格和数量,计算折扣后的小计,并累加得到商品总额。-同时计算数量折扣总额(原价-折扣价)。-然后检查优惠券:根据商品总额,自动应用满足条件的最大优惠券(按满减门槛从高到低检查)。-更新结算信息。5.优惠券高亮:-在计算总价时,会设置优惠券的active状态,然后给对应的优惠券元素添加active-coupon类。6.初始化:-在页面加载时,为每个商品行计算初始小计(调用updateSubtotal,传入id)。-然后计算总价。注意:由于商品行已经存在,我们不需要在JS中初始化商品数组,但需要初始化每个商品的小计。代码结构:我们将按照以下步骤编写代码:1.定义优惠券数组(不变)2.页面加载时初始化:为每个商品行更新小计,然后计算总价,并设置弹窗事件。3.实现updateQuantity、updateSubtotal、removeProduct、showNotification、calculateTotal函数。为了简单,我们不会创建复杂的对象,而是直接操作DOM。由于是入门级别,代码将尽量使用基础语法。开始编写代码:</think>我将基于您的HTML结构实现购物车功能,使用简单的JavaScript代码,直接从DOM读取商品信息,适合入门级别学习。以下是完整的解决方案: ```html <script> // 页面加载完成后执行 window.onload = function() { // 初始化弹窗控制 setupPopup(); // 初始化所有商品的小计 initProducts(); // 计算初始总价 calculateTotal(); }; // 设置弹窗控制 function setupPopup() { // 获取结算按钮 var btn = document.getElementById("jsbtn"); // 获取关闭按钮 var span = document.querySelector('.close'); // 获取弹窗元素 var tc = document.getElementById("tc"); // 点击结算按钮打开弹窗 if (btn) { btn.onclick = function() { tc.style.display = "block"; } } // 点击关闭按钮隐藏弹窗 if (span) { span.onclick = function() { tc.style.display = "none"; } } } // 初始化所有商品的小计 function initProducts() { // 为每个商品计算初始小计 for (var i = 1; i <= 10; i++) { updateSubtotal(i); } } // 更新商品数量 function updateQuantity(productId, change) { // 获取数量输入框 var qtyInput = document.querySelector('#product' + productId + ' .quantity-input'); if (!qtyInput) return; // 获取当前数量 var currentQty = parseInt(qtyInput.value); // 更新数量(不能小于0) var newQty = currentQty + change; if (newQty < 0) newQty = 0; // 更新页面显示 qtyInput.value = newQty; // 更新小计 updateSubtotal(productId); // 重新计算总价 calculateTotal(); } // 更新商品小计 function updateSubtotal(productId) { // 获取商品行 var productRow = document.getElementById('product' + productId); if (!productRow) return; // 获取价格元素 var priceElement = productRow.querySelector('.original-price'); if (!priceElement) return; // 获取价格(去掉¥符号) var price = parseFloat(priceElement.textContent.replace('¥', '')); // 获取数量输入框 var qtyInput = productRow.querySelector('.quantity-input'); if (!qtyInput) return; // 获取当前数量 var quantity = parseInt(qtyInput.value); // 计算折扣(2件9折,3件及以上8折) var discount = 1.0; if (quantity >= 3) { discount = 0.8; // 3件及以上8折 } else if (quantity === 2) { discount = 0.9; // 2件9折 } // 计算小计 var subtotal = price * quantity * discount; // 更新小计显示 var subtotalElement = productRow.querySelector('.jg'); if (subtotalElement) { subtotalElement.textContent = '¥' + subtotal.toFixed(2); } } // 删除商品 function removeProduct(productId) { // 获取商品行 var productRow = document.getElementById('product' + productId); if (!productRow) return; // 添加删除动画效果 productRow.style.opacity = '0'; productRow.style.height = '0'; productRow.style.padding = '0'; productRow.style.margin = '0'; productRow.style.border = 'none'; // 显示删除通知 showNotification('商品已删除'); // 延迟后移除元素 setTimeout(function() { // 移除商品行 productRow.remove(); // 重新计算总价 calculateTotal(); }, 500); } // 显示通知消息 function showNotification(message) { // 创建通知元素 var notification = document.createElement('div'); notification.className = 'notification'; notification.textContent = message; // 添加到页面 document.body.appendChild(notification); // 显示通知 notification.style.opacity = '1'; // 2秒后移除通知 setTimeout(function() { notification.style.opacity = '0'; setTimeout(function() { notification.remove(); }, 500); }, 2000); } // 计算总价和应用优惠券 function calculateTotal() { // 商品总额 var total = 0; // 数量折扣总额 var quantityDiscount = 0; // 商品总数 var totalItems = 0; // 遍历所有商品行(1到10) for (var i = 1; i <= 10; i++) { var productRow = document.getElementById('product' + i); // 如果商品行不存在(已被删除),跳过 if (!productRow) continue; // 获取价格元素 var priceElement = productRow.querySelector('.original-price'); if (!priceElement) continue; // 获取价格(去掉¥符号) var price = parseFloat(priceElement.textContent.replace('¥', '')); // 获取数量输入框 var qtyInput = productRow.querySelector('.quantity-input'); if (!qtyInput) continue; // 获取当前数量 var quantity = parseInt(qtyInput.value); // 计算折扣(2件9折,3件及以上8折) var discount = 1.0; if (quantity >= 3) { discount = 0.8; } else if (quantity === 2) { discount = 0.9; } // 计算原价小计 var originalSubtotal = price * quantity; // 计算折扣后小计 var discountedSubtotal = originalSubtotal * discount; // 累加到总额 total += discountedSubtotal; // 累加数量折扣 quantityDiscount += (originalSubtotal - discountedSubtotal); // 累加商品总数 totalItems += quantity; } // 应用优惠券 var couponDiscount = 0; // 按阈值从大到小检查优惠券 if (total >= 500) { couponDiscount = 90; // 满500减90 highlightCoupon(3); } else if (total >= 200) { couponDiscount = 30; // 满200减30 highlightCoupon(2); } else if (total >= 100) { couponDiscount = 10; // 满100减10 highlightCoupon(1); } else { // 没有适用的优惠券,重置所有优惠券高亮 resetCouponHighlights(); } // 计算应付金额 var payable = total - couponDiscount; // 更新结算信息 document.getElementById('zongjia').textContent = '¥' + total.toFixed(2); document.getElementById('shuliang').textContent = '-¥' + quantityDiscount.toFixed(2); document.getElementById('manjian').textContent = '-¥' + couponDiscount.toFixed(2); document.getElementById('yinfu').textContent = '¥' + payable.toFixed(2); // 更新弹窗中的结算信息 document.getElementById('zongshu').textContent = totalItems; } // 高亮显示激活的优惠券 function highlightCoupon(couponId) { // 重置所有优惠券高亮 resetCouponHighlights(); // 高亮指定优惠券 var couponElement = document.getElementById('yhq' + couponId); if (couponElement) { couponElement.classList.add('active-coupon'); } } // 重置所有优惠券高亮 function resetCouponHighlights() { // 移除所有优惠券的高亮效果 for (var i = 1; i <= 3; i++) { var couponElement = document.getElementById('yhq' + i); if (couponElement) { couponElement.classList.remove('active-coupon'); } } } </script> <style> /* 添加必要的CSS样式 */ .notification { position: fixed; top: 20px; right: 20px; padding: 10px 20px; background-color: #ff5555; color: white; border-radius: 5px; z-index: 1000; opacity: 0; transition: opacity 0.5s; } .active-coupon { background-color: #ffcc00 !important; border: 2px solid #ff6600 !important; box-shadow: 0 0 10px rgba(255,102,0,0.5); } </style> ``` ### 代码解释: 1. **初始化功能**: ```javascript window.onload = function() { setupPopup(); // 设置弹窗控制 initProducts(); // 初始化商品小计 calculateTotal(); // 计算初始总价 }; ``` - 页面加载时自动执行必要的初始化操作 2. **弹窗控制**: ```javascript function setupPopup() { var btn = document.getElementById("jsbtn"); var span = document.querySelector('.close'); var tc = document.getElementById("tc"); btn.onclick = function() { tc.style.display = "block"; } span.onclick = function() { tc.style.display = "none"; } } ``` - 绑定结算按钮和关闭按钮的事件 3. **数量更新**: ```javascript function updateQuantity(productId, change) { var qtyInput = document.querySelector('#product' + productId + ' .quantity-input'); var newQty = parseInt(qtyInput.value) + change; if (newQty < 0) newQty = 0; qtyInput.value = newQty; updateSubtotal(productId); calculateTotal(); } ``` - 处理增减按钮点击 - 限制最小数量为0 - 更新小计和总价 4. **小计计算**: ```javascript function updateSubtotal(productId) { var productRow = document.getElementById('product' + productId); var price = parseFloat(productRow.querySelector('.original-price').textContent.replace('¥', '')); var quantity = parseInt(productRow.querySelector('.quantity-input').value); var discount = 1.0; if (quantity >= 3) discount = 0.8; else if (quantity === 2) discount = 0.9; var subtotal = price * quantity * discount; productRow.querySelector('.jg').textContent = '¥' + subtotal.toFixed(2); } ``` - 直接从DOM读取价格和数量 - 应用数量折扣规则(2件9折,3件及以上8折) - 更新小计显示 5. **商品删除**: ```javascript function removeProduct(productId) { var productRow = document.getElementById('product' + productId); productRow.style.opacity = '0'; productRow.style.height = '0'; showNotification('商品已删除'); setTimeout(function() { productRow.remove(); calculateTotal(); }, 500); } ``` - 添加删除动画效果 - 显示通知提示 - 延迟后移除元素并重新计算总价 6. **总价计算**: ```javascript function calculateTotal() { var total = 0; var quantityDiscount = 0; var totalItems = 0; for (var i = 1; i <= 10; i++) { var productRow = document.getElementById('product' + i); if (!productRow) continue; // 计算每个商品的折扣和小计... } // 应用优惠券... // 更新结算信息... } ``` - 遍历所有商品行 - 计算总额、数量折扣和商品总数 - 自动应用符合条件的优惠券 - 更新页面显示 7. **优惠券处理**: ```javascript function highlightCoupon(couponId) { resetCouponHighlights(); document.getElementById('yhq' + couponId).classList.add('active-coupon'); } function resetCouponHighlights() { for (var i = 1; i <= 3; i++) { var couponElement = document.getElementById('yhq' + i); if (couponElement) couponElement.classList.remove('active-coupon'); } } ``` - 自动应用满足条件的最大优惠券 - 使用CSS类高亮激活的优惠券 ### 特点: 1. **完全基于DOM操作**: - 直接从HTML元素读取商品信息 - 不需要额外的数据数组 - 适合入门学习 2. **简单易懂**: - 使用基础JavaScript语法 - 清晰的函数分工 - 详细的注释说明 3. **保持原有结构**: - 没有更改任何HTML元素的ID或类名 - 完全兼容现有HTML结构 4. **完整功能实现**: - 商品数量管理 ✓ - 折扣系统 ✓ - 优惠券自动应用 ✓ - 结算功能 ✓ - 即时反馈和动画 ✓ 此解决方案完全符合您的需求,使用简单直接的JavaScript代码,适合初学者理解和学习。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值